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"?>
<TextView
 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 :
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="customTheme" parent="android:Theme">
    <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
    </style>
</resources>
4.) Define themes.xml in res/values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="WindowTitleBackground">  
        <item name="android:background">@color/titlebackgroundcolor</item>    
    </style>
</resources>
5.) Define styles.xml in res/values:
<?xml version="1.0" encoding="utf-8"?>
<resources> 
    <color name="titlebackgroundcolor">#3232CD</color>
    <color name="titletextcolor">#FFFF00</color>
</resource>
6.) Put the following code in AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.app.CustomTitleBar"
     android:versionCode="1"
     android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CustomTitleBar"android:theme="@style/customTheme"
                 android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
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:

No comments:

Post a Comment