Saturday, September 8, 2012

Creating a custom title bar In Android

This is a sample application which shows how to create a custom Title Bar in android. Last topic published on this forum is Create a Toggle Button.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project CustomTitleBar.
2.) Define a custom layout mytitle.xml in res/layout :
<?xml version="1.0" encoding="utf-8"?>

 android:text="custom title bar"
 android:gravity ="center"/>
3.) Define colors.xml in res/values :
<?xml version="1.0" encoding="utf-8"?>
    <style name="customTheme" parent="android:Theme">
    <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
4.) Define themes.xml in res/values:
<?xml version="1.0" encoding="utf-8"?>
   <style name="WindowTitleBackground">  
        <item name="android:background">@color/titlebackgroundcolor</item>    
5.) Define styles.xml in res/values:
<?xml version="1.0" encoding="utf-8"?>
    <color name="titlebackgroundcolor">#3232CD</color>
    <color name="titletextcolor">#FFFF00</color>
6.) Put the following code in AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    <uses-sdk android:minSdkVersion="7" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CustomTitleBar"android:theme="@style/customTheme"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
7.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. CustomTitleBar. Enter following information:
Project name: CustomTitleBar
Build Target: Android 2.1
Application name: CustomTitleBar
Package name:
Create Activity: CustomTitleBar
On Clicking Finish CustomTitleBar code structure is generated with the necessary Android Packages being imported along with CustomTitleBar class will look like following:
import android.os.Bundle;
import android.view.Window;
public class CustomTitleBar extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
Output –The final output:

No comments:

Post a Comment