Custom title is nothing but a layout you bind to your application's title bar.
MainActivity.java
package com.example.customtitlebar;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customheader);
init();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customheader);
init();
}
private void init() {
// TODO Auto-generated method stub
Button Left=(Button)findViewById(R.id.header_left_btn);
Left.setText("BACK");
Left.setTextColor(Color.GRAY);
Button Title=(Button)findViewById(R.id.header_text);
Title.setText("Scorecard");
Title.setTextColor(Color.GRAY);
Button Right=(Button)findViewById(R.id.header_right_btn);
Right.setText("NEXT");
Right.setTextColor(Color.GRAY);
// TODO Auto-generated method stub
Button Left=(Button)findViewById(R.id.header_left_btn);
Left.setText("BACK");
Left.setTextColor(Color.GRAY);
Button Title=(Button)findViewById(R.id.header_text);
Title.setText("Scorecard");
Title.setTextColor(Color.GRAY);
Button Right=(Button)findViewById(R.id.header_right_btn);
Right.setText("NEXT");
Right.setTextColor(Color.GRAY);
/*If you want attach icon with Title text add below line in code.
Title.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_launcher, 0, 0, 0);
Title.setCompoundDrawablesWithIntrinsicBounds( R.drawable.ic_launcher, 0, 0, 0);
*/
/*
You just need to pass drawable resources to this method.
setCompoundDrawablesWithIntrinsicBounds(left,top,right,bottom)
*/
}
}
}
}
mainactivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.customtitlebar.MainActivity"
tools:ignore="MergeRootFrame" />
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.customtitlebar.MainActivity"
tools:ignore="MergeRootFrame" />
This is the layout which define how your custom title bar will look like.
customheader.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="50dp"
android:layout_gravity="fill_horizontal"
android:layout_width="fill_parent"
android:background="@android:color/holo_blue_light"
android:orientation="horizontal">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="50dp"
android:layout_gravity="fill_horizontal"
android:layout_width="fill_parent"
android:background="@android:color/holo_blue_light"
android:orientation="horizontal">
<Button
android:id="@+id/header_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:background="@android:color/transparent"
android:textColor="@android:color/white" />
android:id="@+id/header_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textStyle="bold"
android:background="@android:color/transparent"
android:textColor="@android:color/white" />
<Button
android:id="@+id/header_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:textAlignment="center"
android:textSize="18sp"
android:background="@android:color/transparent"
android:textColor="@android:color/white"
android:textStyle="bold" />
android:id="@+id/header_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:textAlignment="center"
android:textSize="18sp"
android:background="@android:color/transparent"
android:textColor="@android:color/white"
android:textStyle="bold" />
<Button
android:id="@+id/header_right_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@android:color/transparent"
android:textColor="@android:color/white"
android:textStyle="bold" />
android:id="@+id/header_right_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@android:color/transparent"
android:textColor="@android:color/white"
android:textStyle="bold" />
</RelativeLayout>
To apply your own custom title bar you need to create your own theme in theme.xml file.
theme.xml
theme.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
<resources>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>
</resources>
</resources>
Apply style for your custom title bar in style.xml
style.xml
add below line in style.xml file
<style name="WindowTitleBackground" >
<item name="android:background">@android:color/transparent</item>
</style>
<item name="android:background">@android:color/transparent</item>
</style>
And main part of applying custom title bar is you have to change theme of your activity in which you are applying custom title bar.
AndroidManifest.xml
Just add android:theme="@style/MyTheme" line in activity tag.
<activity
android:name="com.example.customtitlebar.MainActivity"
android:label="@string/app_name"
android:theme="@style/MyTheme">
android:name="com.example.customtitlebar.MainActivity"
android:label="@string/app_name"
android:theme="@style/MyTheme">
That's it you are done with the custom title bar in android.
Here is the sample Code
Comments
Post a Comment