32

I have created an Activity and declared in Manifest file. But I would like to re-use the same Activity for other purpose.

 <activity
            android:configChanges="orientation|keyboardHidden"
            android:label="Main Menu"
            android:name=".MainMenu"
            android:theme="@android:style/Theme.Light" >
        </activity>

I need to change the Label dynamically. Thanks in Advance

SamSPICA
  • 1,366
  • 15
  • 31
noname
  • 423
  • 1
  • 6
  • 9

10 Answers10

72

Use

setTitle(int titleId)

or

setTitle(CharSequence title)
Garg
  • 2,731
  • 2
  • 36
  • 47
Shubhayu
  • 13,402
  • 5
  • 33
  • 30
23
public class YourActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      setTitle(R.string.your_title);
      setContentView(R.layout.main);
  }
}
SamSPICA
  • 1,366
  • 15
  • 31
  • Is putting it before `setContentView()` important? I noticed there's another question just like this one where using `setTitle()` wasn't working for him, but he used it after. – Jacob Zimmerman Mar 02 '17 at 02:02
  • I did not face such a scenario, but it might be negligible amount of work to try out both the scenario. If it is indeed the case, and if that person needs to set the title only after the `setContentView()`, he might set a dummy string to the title first, and then proceed with changing it in the program flow. – SamSPICA Mar 03 '17 at 16:56
  • 2
    @JacobZimmerman It is and this answer solved my problem. With doing it in this order, TalkBack will read the correct title to the user! – Pedram Aug 16 '17 at 20:39
9

You need to use setTitle for this:

setTitle(R.string.your_title);
//or
setTitle("new title");
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • 1
    SetTitleColor can also be used in the onCreate() method to make your title a particular color. setTitleColor(getResources().getColor(R.color.mycolor)); – Tastybrownies Nov 12 '13 at 05:08
4

You can define the string in res/string.xml file and then add android:label to the AndroidMenifest.xml file

string.xml code
<string name="string_name">text to display</string>

AndroidMenifest.xml code

<activity android:name=".SomeActivity"
            android:label="@string/string_name"/>
goto
  • 7,908
  • 10
  • 48
  • 58
3

I have the same problem, Try this in onCreate it's work for me!

public class YourActivityName extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    setTitle("Your Title");
  }
}
1
if(Condition)
{
    setTitle("Your Title");
}
else
{
    // your Default Title from Manifest
}

Try to use this line.

Bhavin
  • 6,020
  • 4
  • 24
  • 26
0

Static, not Dynamic

Go to your AndroidManifest.xml file and add the android:label attribute to the activity tag you want, like this:

<activity android:name=".name" android:label="label"/>
ryanwaite28
  • 1,804
  • 2
  • 24
  • 40
0

In your Manifest file

Initially, your code was like this.

    <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".NumbersActivity"  />
    <activity android:name=".FamilyMembersActivity"  />
    <activity android:name=".ColorsActivity"  />
    <activity android:name=".PhrasesActivity" />
    </activity>
</application>

But after changes to add Labels.

<activity android:name=".NumbersActivity" android:label="Numbers" />
    <activity android:name=".FamilyMembersActivity" android:label="Family Members" />
    <activity android:name=".ColorsActivity" android:label="Colors" />
    <activity android:name=".PhrasesActivity" android:label="Phrases" >
    </activity>
</application>

is the optimum way to change the Label name.

courtesy.(ryanwaite28)

Rahul Joshi
  • 263
  • 3
  • 10
0

User this

@Override
  public void onCreate(Bundle savedInstanceState) {
    setTitle("Your Title");
  }
Aleks
  • 1
-2

android:label="any word you want"/>

  • 1
    This row of code is for AndroidManifest. And this will not allow you to change the title dynamically. "to change the Label dynamically" You shall use setTitle() method. – Rahim Rahimov May 25 '18 at 20:02