MY mENU


Tuesday, 18 September 2012

Custome Titile 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 :
 version="1.0" encoding="utf-8"?>

 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/myTitle"
 android:text="custom title bar"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:textColor="@color/titletextcolor"
 android:gravity ="center"/>
3.) Define colors.xml in res/values :
 version="1.0" encoding="utf-8"?>
>

    >

>
4.) Define themes.xml in res/values:
 version="1.0" encoding="utf-8"?>
>

   >

>
5.) Define styles.xml in res/values:
 version="1.0" encoding="utf-8"?>
>

     name="titlebackgroundcolor">#3232CD
>
     name="titletextcolor">#FFFF00>
>
6.) Put the following code in AndroidManifest.xml:
 version="1.0" encoding="utf-8"?>
 xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.app.CustomTitleBar"
     android:versionCode="1"
     android:versionName="1.0">
     android:minSdkVersion="7" />

     android:icon="@drawable/icon" android:label="@string/app_name">
         android:name=".CustomTitleBar"android:theme="@style/customTheme"
                 android:label="@string/app_name">
            >

                 android:name="android.intent.action.MAIN" />
                 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: com.app.CustomTitleBar
Create Activity: CustomTitleBar
On Clicking Finish CustomTitleBar code structure is generated with the necessary Android Packages being imported along with CustomTitleBar.java. CustomTitleBar class will look like following:
package com.app.CustomTitleBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class CustomTitleBar 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.mytitle);
    }
}
Output –The final output: