views:

793

answers:

2

How do I change the text of the title bar? as of now it just displays the title of the program and im wanting it to display something of my choosing and be different for each page/activity in my app i.e. my home page could say page1 in the title bar while another activity that the app switches to could have page2 in that page's title bar.

+2  A: 

You can define the label for each activity in your manifest file.

A normal definition of a activity looks like this:

<activity
     android:name=".ui.myactivity"
     android:label="@string/Title Text" />

Where title text should be replaced by the id of a string resource for this activity.

You can also set the title text from code if you want to set it dynamically.

setTitle(address.getCity());

with this line the title is set to the city of a specific adress in the oncreate method of my activity.

Janusz
+12  A: 

=> Normal way,

you can Change the Title of each screen (i.e. Activity) by setting their Android:label

   <activity android:name=".Hello_World"
                  android:label="This is the Hello World Application">
   </activity>

=> Custom - Title - bar


But if you want to Customize title-bar in your way, i.e. Want to put Image icon and custom-text, then following code is running for me:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

</LinearLayout>

titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="400px" 
  android:layout_height="fill_parent"
  android:orientation="horizontal">

<ImageView android:id="@+id/ImageView01" 
            android:layout_width="57px" 
            android:layout_height="wrap_content"
            android:background="@drawable/icon1">

</ImageView>

<TextView 

  android:id="@+id/myTitle" 
  android:text="This is my new title" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:textColor="@color/titletextcolor"
   />
</LinearLayout>

TitleBar.java

public class TitleBar extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);

           final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

           setContentView(R.layout.main);


           if ( customTitleSupported ) {
               getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
               }

           final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
           if ( myTitleText != null ) {
               myTitleText.setText("NEW TITLE");

               // user can also set color using "Color" and then "Color value constant"
              // myTitleText.setBackgroundColor(Color.GREEN);
           }
   }
}

strings.xml

The strings.xml file is defined under the values folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Set_Text_TitleBar!</string>
    <string name="app_name">Set_Text_TitleBar</string>

    <color name="titlebackgroundcolor">#3232CD</color>
    <color name="titletextcolor">#FFFF00</color>

</resources>
PM - Paresh Mayani
i have added strings.xml formatting
PM - Paresh Mayani
Follow this example to set Custom Title in android for an activity or globally for entire application : http://labs.makemachine.net/2010/03/custom-android-window-title/
PM - Paresh Mayani