How to change title of Activity in Android?

AndroidAndroid Activity

Android Problem Overview


I am using

Window w = getWindow();
w.setTitle("My title");
	

to change title of my current Activity but it does not seem to work.

Can anyone guide me on how to change this?

Android Solutions


Solution 1 - Android

Try setTitle by itself, like this:

setTitle("Hello StackOverflow");

Solution 2 - Android

Just an FYI, you can optionally do it from the XML.

In the AndroidManifest.xml, you can set it with

android:label="My Activity Title"

Or

android:label="@string/my_activity_label"

Example:

    <activity
        android:name=".Splash"
        android:label="@string/splash_activity_title" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Solution 3 - Android

If you want it one time & let system handle the rest (not dynamic) then do like this in your manifest file:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name_full" > //This is my custom title name on activity. <- The question is about this one.
            <intent-filter android:label="@string/app_launcher_name" > //This is my custom Icon title name (launcher name that you see in android apps/homescreen)
                <action android:name="android.intent.action.MAIN" />

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

Solution 4 - Android

setTitle(getResources().getText(R.string.MyTitle));

Solution 5 - Android

There's a faster way, just use

YourActivity.setTitle("New Title");

You can also find it inside the onCreate() with this, for example:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setTitle("My Title");
    }

By the way, what you simply cannot do is call setTitle() in a static way without passing any Activity object.

Solution 6 - Android

This worked for me.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
	View v = inflater.inflate(R.layout.fragment, container, false);
	getActivity().setTitle("My Title");
//...
}

Solution 7 - Android

If you have multiple activities, you can set it like this in AndroidManifest.xml

http://schemas.android.com/apk/res/android" package="com.example.android.miwok">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".NumbersActivity"
        android:label="@string/category_numbers"
        android:theme="@style/category_numbers" />
    <activity
        android:name=".FamilyActivity"
        android:label="@string/category_family"
        android:theme="@style/category_family" />
    <activity
        android:name=".ColorsActivity"
        android:label="@string/category_colors"
        android:theme="@style/category_colors" />
    <activity
        android:name=".PhrasesActivity"
        android:label="@string/category_phrases"
        android:theme="@style/category_phrases" />
    <activity
        android:name=".ExperimentActivity"
        android:label="@string/category_experiment"
        android:theme="@style/category_experiment" />
</application>

Solution 8 - Android

I'm using Android Studio 3.0.1.

WIth an Activity:

setTitle("Title Text");

Inside a fragment:

getActivity().setTitle("Title Text");

Solution 9 - Android

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.Main_Activity);
    this.setTitle("Title name");
}

Solution 10 - Android

If you want to set title in Java file, then write in your activity onCreate

setTitle("Your Title");

if you want to in Manifest then write

    <activity
        android:name=".MainActivity"
        android:label="Your Title" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Solution 11 - Android

I have a Toolbar in my Activity and a Base Activity that overrides all Titles. So I had to use setTitle in onResume() in the Activity like so:

@Override
  protected void onResume() {
    super.onResume();
    toolbar.setTitle(R.string.title);
  }

Solution 12 - Android

The code helped me change the title.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);
    ActivityName.this.setTitle("Your Activity Title");}

Solution 13 - Android

Inside a MainActivity:

public class act1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act1);

    setTitle("First Activity");

}

}

Solution 14 - Android

setTitle("Whatever apps"); in MainActivity.java is simplier I would say.

Solution 15 - Android

If you want to change Title of activity when you change activity by clicking on the Button. Declare the necessary variables in MainActivity:

    private static final String TITLE_SIGN = "title_sign";
    ImageButton mAriesButton;

Add onClickListener in onCreate() and make new intent for another activity:

    mTitleButton = (ImageButton) findViewById(R.id.title_button);
    mTitleButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, 
        SignActivity.class);
        String title_act = getText(R.string.simple_text).toString();
        intent.putExtra("title_act", title_act);
        startActivity(intent);
        finish();
        }
    });

SecondActivity code in onCreate():

    String txtTitle = getIntent().getStringExtra("title_act");
    this.setTitle(txtTitle);

Solution 16 - Android

If you're using onCreateOptionsMenu, you can also add setTitle code in onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    setTitle("Neue Aktivität");
    return true;
}

Solution 17 - Android

setTitle("Welcome Activity");

Solution 18 - Android

In Kotlin, this way:

this.title = resources.getText(R.string.fast_learning)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUMAR-MOBITSOLUTIONSView Question on Stackoverflow
Solution 1 - AndroidjeffhView Answer on Stackoverflow
Solution 2 - AndroidBullSharkView Answer on Stackoverflow
Solution 3 - AndroidhB0View Answer on Stackoverflow
Solution 4 - AndroidGennady KozlovView Answer on Stackoverflow
Solution 5 - AndroidDefragView Answer on Stackoverflow
Solution 6 - AndroidmobermeView Answer on Stackoverflow
Solution 7 - AndroidSGXView Answer on Stackoverflow
Solution 8 - AndroidadrianView Answer on Stackoverflow
Solution 9 - AndroidGovind MaheshwariView Answer on Stackoverflow
Solution 10 - Androiduser6395256View Answer on Stackoverflow
Solution 11 - AndroidHissatsuView Answer on Stackoverflow
Solution 12 - AndroidRohan GuptaView Answer on Stackoverflow
Solution 13 - AndroidYash HiraparaView Answer on Stackoverflow
Solution 14 - AndroidStephane MarchandView Answer on Stackoverflow
Solution 15 - AndroidEgor VoevodinView Answer on Stackoverflow
Solution 16 - AndroidUmut D.View Answer on Stackoverflow
Solution 17 - AndroidRaihan MahamudView Answer on Stackoverflow
Solution 18 - AndroidMoriView Answer on Stackoverflow