how to override default title with custom title in android
Asked Answered
L

1

6

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?

Lach answered 7/12, 2011 at 7:37 Comment(6)
just a random thought, do you have tried to put "setContentView" as the last line of the onCreate method?Tumbrel
is it needed to write custom title if no then change this line android:text="custom title bar" to android:text=""Prepense
@STT LCU & user370305 : i had already tried it. while i am coding like, getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.window_title); setContentView(R.layout.main); it shows error. but if i put it as i had done in question, it works niceLach
@kelsi: actually i have to add a label and 2 icons in it. i had just tried to get an idea befor i do the complex things.Lach
try android:layout_height="wrap_content" for textviewReube
i think i had done the same in mu window_title.xml fileLach
M
1

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

Maxantia answered 7/12, 2011 at 8:32 Comment(5)
can u just describe how i can do it? i am new to android. just studying.i think i have created my title bar. and its working fine. will u check my code and suggest where i am wrong?Lach
put the above line in you onCreate();Girlish
@April and sam.Janz: no dear... it also doesnt works... i had tried by putting that code in my oncreate. when i does so, it catches errorLach
@sam.janz: I think actually you are not my question right. i know that is possible as u described... I am a student, and want to study about how custom title works. so just wish to know how i works. Thanks dear...Lach
i am sure that ur code does work well. but actually now i as i am studying the custom title.. i wish to get more idea on that..... anyway thank u for finding time to help me dearLach

© 2022 - 2024 — McMap. All rights reserved.