Sunday, July 15, 2012

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 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android: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. */
    @Override
    public 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);     
    }
}

0 comments :

Post a Comment

Contact Form

Name

Email *

Message *

Back to Top