244

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?

Melquiades
  • 8,496
  • 1
  • 31
  • 46
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

18 Answers18

531

Try setTitle by itself, like this:

setTitle("Hello StackOverflow");
jeffh
  • 614
  • 2
  • 9
  • 20
263

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>
Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
BullShark
  • 85
  • 2
  • 4
  • 12
  • 32
    Why would you vote this down? It's good to know you can do it from the XML also. – BullShark Apr 06 '13 at 12:44
  • 9
    This is pretty much the corect way to do it, with references to external strings to enable easy localisation. Should be accepted answer. – Davor Jul 16 '13 at 09:13
  • 12
    It's not the same as setTitle(). Setting the android:label property of the launcher activity will also change the name of the application on the phone's applications screen. Your application's icon will have "My Activity Title" caption. – Doron Zehavi Apr 07 '14 at 11:58
  • 2
    @doran While you are correct, and his example is wrong in a way, he is also right. His issue is adding a label to a splash screen, and you wouldn't do that for a splash screen, rather just label it with the app name. However the label for the whole application is what determines the title given to the launcher if it is set – Pazuzu156 Jul 14 '14 at 17:50
  • 1
    This should be the best answer. – MaXi32 Mar 11 '17 at 09:23
  • On at least my version of Android Studio, this also changes the name of the app on the home screen, which is not a great idea if you're trying to make the menu title go away with an empty string. :-) – John Perry Jul 18 '17 at 12:12
  • I had to add android:label="@string/my_activity_label" instead of @strings! – maracuja-juice Nov 01 '17 at 08:26
  • @BullShark The original question is "How to change title of Activity in Android?". "change" can be interpreted several ways, but based on the OP's use of code I presume he wanted to change the title programmatically. Your example is hard coded. Thus why you might have received down votes (not from me). – swooby Aug 29 '19 at 21:15
29

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>
hB0
  • 1,977
  • 1
  • 27
  • 33
16
setTitle(getResources().getText(R.string.MyTitle));
FanaticD
  • 1,416
  • 4
  • 20
  • 36
Gennady Kozlov
  • 1,011
  • 1
  • 11
  • 11
  • @Sujay yes, you can, here you are: try { int labelRes = getPackageManager().getActivityInfo(getComponentName(), 0).labelRes; Logger.d(this.getClass().getSimpleName(), "labelRes : " + labelRes); if (labelRes > 0 && getSupportActionBar() != null) { getSupportActionBar().setTitle(labelRes); } } catch (PackageManager.NameNotFoundException exception) { Logger.d(this.getClass().getSimpleName(), "exception caught : " + Log.getStackTraceString(exception)); } – user1510006 Sep 13 '17 at 14:15
10

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.

Defrag
  • 406
  • 7
  • 12
  • this doesn't work when pressing the standby button and making the screen active again. The title does not update. Any ideas? – Jim Clermonts Nov 07 '17 at 15:30
  • That's because onCreate() won't be called in your example. You should consider using another event handler instead, such as onResume(). I suggest you to see this link: https://developer.android.com/guide/components/activities/activity-lifecycle.html – Defrag Dec 07 '17 at 18:20
9

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");
//...
}
moberme
  • 669
  • 7
  • 13
5

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

<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>
SGX
  • 1
  • 1
  • 2
  • i did exactly like this, it did not work. on every activity i am seeing just App Name. Unable to change it. is been now more than half an hour :( – krupesh Anadkat Dec 06 '18 at 14:29
4

I'm using Android Studio 3.0.1.

WIth an Activity:

setTitle("Title Text");

Inside a fragment:

getActivity().setTitle("Title Text");
Derlin
  • 9,572
  • 2
  • 32
  • 53
adrian
  • 1
  • 1
4
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.Main_Activity);
    this.setTitle("Title name");
}
2

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>
Gundu Bandgar
  • 2,593
  • 1
  • 17
  • 21
2

In Kotlin, this way:

this.title = resources.getText(R.string.fast_learning)
Mori
  • 2,653
  • 18
  • 24
1

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);
  }
Hissatsu
  • 38
  • 8
1

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

Inside a MainActivity:

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

    setTitle("First Activity");

}

}

1

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

0

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);
0

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;
}
Umut D.
  • 1,746
  • 23
  • 24
0

setTitle("Welcome Activity");