Android: Changing the Title of an Activity – setTitle works – android:label does not?

To change the title of an activity (to show a more custom title for a certain activity than the application title), you can set the new title either through calling setTitle("foo") on the activity or by setting android:label="@string/price_after_rebate" in your activity style.

The problem was that the latter didn’t work, while the first one did. I try to keep any static definitions related to the activities outside of the code itself, but that’s hard when it doesn’t work as expected.

Turns out that if there’s a title set in the AndroidManifest.xml file (located under app/manifests/ in the standard layout / Android Studio), it’ll override any title set elsewhere in the definitions. You can change the specific titles by setting android:label="@string/price_after_rebate" on the activity definitions in the manifest instead of the activity xml file:

<activity
    android:name=".xyz.Foo"
    android:parentActivityName=".MainActivity"
    android:label="@string/xyz_foo_activity_title"
>
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="xyz.MainActivity" />
</activity>

7 thoughts on “Android: Changing the Title of an Activity – setTitle works – android:label does not?”

  1. That probably means its time to post a question on Stack Overflow with a minimal example attached.

  2. It’s literally the first paragraph in the post (I assume you meant label, not level):

    To change the title of an activity (to show a more custom title for a certain activity than the application title), you can set the new title either through calling setTitle(“foo”) on the activity

  3. one small doubt ,that is i need to display

    name:xyz

    Address :asd
    Rollnumber:1
    what is the code in android

  4. This solution works if you re finding that you can set the title once, but can t change it after that. Make sure you re setting the title of the Collapsing Toolbar. Hylianpuffball Nov 27 ’18 at 16:13

Leave a Reply

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