Currently it just displays the name of the application and I want it to display something custom and be different for each screen in my app.
For example: my home scr
The best way to change the action bar name is to go to the AndroidManifest.xml and type this code.
<activity android:name=".YourActivity"
android:label="NameYouWantToDisplay> </activity>
We can change the ActionBar title in one of two ways:
In the Manifest: in the manifest file, set the label of each Activity.
android:label="@string/TitleWhichYouWantToDisplay"
In code: in code, call the setTitle() method with a String or the id of String as the argument.
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.TitleWhichYouWantToDisplay);
// OR You can also use the line below
// setTitle("MyTitle")
setContentView(R.layout.activity_main);
}
}
You can define your title programatically using setTitle
within your Activity
, this method can accept either a String
or an ID defined in your values/strings.xml
file. Example:
public class YourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.your_title);
setContentView(R.layout.main);
}
}
The easiest way is to call this.setTitle("...")
if you are in the activity.
And if you are in a fragment, just call getActivity().setTitle("...")
;
This way will let you change the title anytime, no need to call it before setContentView(R.layout.activity_test)
;
getActionBar().setTitle("edit your text");
getSupportActionBar().setTitle("title");