How to change title of Activity in Android?
I am using
Window w = getWindow();
w.setTitle("My title");
to change title of my current Activity but it d开发者_运维百科oes not seem to work.
Can anyone guide me on how to change this?
Try setTitle by itself, like this:
setTitle("Hello StackOverflow");
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>
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>
setTitle(getResources().getText(R.string.MyTitle));
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.
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");
//...
}
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>
I'm using Android Studio 3.0.1.
WIth an Activity:
setTitle("Title Text");
Inside a fragment:
getActivity().setTitle("Title Text");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Main_Activity);
this.setTitle("Title name");
}
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>
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);
}
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");}
Inside a MainActivity:
public class act1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
setTitle("First Activity");
}
}
setTitle("Whatever apps"); in MainActivity.java is simplier I would say.
In Kotlin, this way:
this.title = resources.getText(R.string.fast_learning)
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);
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;
}
setTitle("Welcome Activity");
精彩评论