I am using
Window w = getWindow();
w.setTitle(\"My title\");
to change title of my current Activity but it does not seem to work.
Try setTitle by itself, like this:
setTitle("Hello StackOverflow");
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);
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'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;
}
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>
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>