Android custom title bar

Android custom title bar

This article provides an example to include an android custom title bar. Android allows to replace the default title bar with a custom title bar. This possible by using the extended window features.

Android application with customer title bar

Implement a layout (custom_title.xml) that needs to be used as the title bar.

<?xml version="1.0" encoding="utf-8"?><LinearLayout android_layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:background="#ff0000"android:text="This is my custom title"android:textAppearance="?android:attr/textAppearanceMedium" /></LinearLayout>

In the onCreate method of the Activity class request for the extended window feature and replace with the new custom title layout.

package com.sourcetricks.CustomHeader;import android.app.Activity;import android.os.Bundle;import android.view.Window;public class CustomHeaderActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);setContentView(R.layout.main);getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);}}