Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to override default title with custom title in android

I have designed a custom title bar for my application. It works well for me too. But there is a problem. The default title is seen (for just a second), before my custom title bar overrides it. Is there any solution to disable the default title bar which android provides?

My codes were like...

window_title.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"/>

color.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>

styles.xml in res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>   
    <color name="titlebackgroundcolor">#303010</color>
    <color name="titletextcolor">#FFFF00</color>
</resources>

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>

And i had also updated my manifest.xml's activity, as

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.title"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:theme="@style/customTheme"
        android:label="@string/app_name" >
        <activity
            android:name=".CustomTitleActivity"
            android:theme="@style/customTheme" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

and on my onCreate method i had done like..

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.window_title);
final TextView myTitle = (TextView) findViewById(R.id.myTitle);
myTitle.setText("Converter");

And this works well too

I had coded as in http://www.edumobile.org/android/android-programming-tutorials/creating-a-custom-title-bar/ and it works well too..

Is there anyway to disable the default title that comes first?

like image 663
Dil Se... Avatar asked Dec 07 '11 07:12

Dil Se...


1 Answers

why you relay on android for title bar just design your title bar as you want and set it at top in your main layout and use following code in onCreate before setting layout

requestWindowFeature(Window.FEATURE_NO_TITLE);

here is example... define your title bar in your main layout like if you are setting main.xml as you activity's layout then your main.xml should be like

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
   //here i have used LinearLayout as example your's can be Relative or whatever

 //here my title bars layout starts containing an icon and a text your's can containing just text or whatever you want
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/my_title_background"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="15dip"
        android:src="@drawable/my_icon" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="15dip"
        android:text="My Application title"
        android:textColor="#FFFFFF" />
</LinearLayout>

   //and here rest of your layut for your application functionality
</LinearLayout>

then in my activities java file which containing onCreate() method the code will be like that

 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    //restof code for my class

you dont need to set any thing else for your title bar like setting themes and setting other features or permissions.......... i hope you got it

like image 127
Umar Qureshi Avatar answered Sep 20 '22 21:09

Umar Qureshi