How to change title of Activity in Android?

前端 未结 14 1189
无人及你
无人及你 2020-12-02 05:30

I am using

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

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

相关标签:
14条回答
  • 2020-12-02 06:17

    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.

    0 讨论(0)
  • 2020-12-02 06:17

    I'm using Android Studio 3.0.1.

    WIth an Activity:

    setTitle("Title Text");
    

    Inside a fragment:

    getActivity().setTitle("Title Text");
    
    0 讨论(0)
  • 2020-12-02 06:18

    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>
    
    0 讨论(0)
  • 2020-12-02 06:19

    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>
    
    0 讨论(0)
  • 2020-12-02 06:23

    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");}
    
    0 讨论(0)
  • 2020-12-02 06:28

    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);
      }
    
    0 讨论(0)
提交回复
热议问题