noname
noname

Reputation: 423

How to change android Activity label

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

Upvotes: 32

Views: 59550

Answers (10)

Aleks
Aleks

Reputation: 1

User this

@Override
  public void onCreate(Bundle savedInstanceState) {
    setTitle("Your Title");
  }

Upvotes: 0

Shubhayu
Shubhayu

Reputation: 13552

Use

setTitle(int titleId)

or

setTitle(CharSequence title)

Upvotes: 72

shoooka
shoooka

Reputation: 1

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

Upvotes: -2

Rahul Joshi
Rahul Joshi

Reputation: 273

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)

Upvotes: 0

user8442578
user8442578

Reputation:

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");
  }
}

Upvotes: 3

ryanwaite28
ryanwaite28

Reputation: 2038

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"/>

Upvotes: 0

Triple Hash
Triple Hash

Reputation: 41

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"/>

Upvotes: 4

SamSPICA
SamSPICA

Reputation: 1366

public class YourActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      setTitle(R.string.your_title);
      setContentView(R.layout.main);
  }
}

Upvotes: 23

Bhavin
Bhavin

Reputation: 6010

if(Condition)
{
    setTitle("Your Title");
}
else
{
    // your Default Title from Manifest
}

Try to use this line.

Upvotes: 1

waqaslam
waqaslam

Reputation: 68167

You need to use setTitle for this:

setTitle(R.string.your_title);
//or
setTitle("new title");

Upvotes: 9

Related Questions