How to change title of Activity in Android?

前端 未结 14 1187
无人及你
无人及你 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:07

    Try setTitle by itself, like this:

    setTitle("Hello StackOverflow");
    
    0 讨论(0)
  • 2020-12-02 06:07

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

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

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

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

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