Custom Title Bar in ANDROID

By | March 29, 2012

Hi all….

Today in this post I will show you how to give a custom layout for the titleBar in ANDROID.
You can place almost anything inside a title using your custom layout.

Now go on and create a fresh project and copy this code into the main java file

package pack.coderzheaven;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CustomTitleDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);

        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
            TextView tv = (TextView) findViewById(R.id.tv);
            tv.setText("CoderzHeaven");

            ProgressBar titleProgressBar = (ProgressBar) findViewById(R.id.PB);
            //titleProgressBar.setVisibility(ProgressBar.GONE);
        }
    }
}

Now create a new xml file and name it custom_title.xml and copy the following code to it.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">.

        <ImageView android:layout_width="40dip"
				android:id="@+id/ImageView01"
				android:background="@drawable/android_2"
				android:layout_height="40dip"
				>
		</ImageView>

        <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tv"
                android:text="CoderzHeaven"
                android:layout_toRightOf="@+id/ImageView01"
                android:textColor="@drawable/red"
                android:textStyle="bold"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip">
        </TextView>

		 <ProgressBar android:id="@+id/PB"
                style="?android:attr/progressBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/tv"
                android:paddingLeft="3dip">
         </ProgressBar>
</RelativeLayout>

Note: remember to have images mentioned here in the drawable folder.

Now the strings.xml file where the color is defined.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Custom Title Bar from CoderzHeaven</string>
    <drawable name="white">#ffffff</drawable>
    <drawable name="black">#000000</drawable>
	<drawable name="blue">#2554C7</drawable>
	<drawable name="green">#347C2C</drawable>
	<drawable name="orange">#ff9900</drawable>
	<drawable name="pink">#FF00FF</drawable>
	<drawable name="violet">#a020f0</drawable>
	<drawable name="grey">#778899</drawable>
	<drawable name="red">#C11B17</drawable>
	<drawable name="yellow">#FFFF8C</drawable>
	<drawable name="PowderBlue">#b0e0e6</drawable>
	<drawable name="brown">#2F1700</drawable>
	<drawable name="Hotpink">#7D2252</drawable>
</resources>

Atlast the AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="CustomTitleDemo">
        <activity android:name=".CustomTitleDemo"
                  android:label="CustomTitleDemo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Custom TitleBar in ANDROID

Custom TitleBar in ANDROID

Please leave your valuable comments if you found this post useful………

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *