Saturday, 12 November 2011

Android - Set title to an Activity

In the activity of Android, the title normally it is the name of the application. Here is the example in the Android Manifest file:

<activity android:name=".MyActivity" android:label="@string/app_name">

But it could be changed using different ways:

1. Change the name of the application in the res/values/string.xml
This could be the easiest way to do it.

For example:
<string name="app_name">My new title</string>

2. Change the value in the manifest file
Because it is described in the manifest file, if we change the value of the label in the manifest, it will be changed also in the activity screen.

For example:
<activity android:name=".MyActivity" android:label="My new title">

3. Change the resource in the manifest file
Because the manifest file just points to a value in the res/values/string.xml, if we create a new resource (string) in the string.xml file and modify the manifest file such way that it points to the new resource, the new resource will be appear as the title of the activity.

For example:
Add the follow line to the file res/values/string.xml
<string name= "new_title"> My new title </string>

and modify the original line in the manifest file
<activity android:name=".MyActivity" android:label="@string/new_title">

4. Change it in the activity
If the title shown in the screen should be accord to any dynamic variable, it could be changed also in the activity.

For example, you can add the follow sentence to onCreate method:
setTitle("My new title");

This method will override the original titile in the manifest file.

For more information:
Android developers - Activity:

No comments:

Post a Comment