[android] How to change the text on the action bar

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 screen could say 'page1' in the action bar while another activity that the app switches to could have 'page2' in that screens action bar.

This question is related to android android-actionbar

The answer is


if u r using navigation bar to change fragment then u can add change it where u r changing Fragment like below example :

 public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.clientsidedrawer:
                    //    Toast.makeText(getApplicationContext(),"Client selected",Toast.LENGTH_SHORT).show();
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new clients_fragment()).commit();
                    break;
    
                case R.id.adddatasidedrawer:
                    getSupportActionBar().setTitle("Add Client");
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new addclient_fragment()).commit();
                    break;
    
                case R.id.editid:
                    getSupportActionBar().setTitle("Edit Clients");
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new Editclient()).commit();
                    break;
    
                case R.id.taskid:
                    getSupportActionBar().setTitle("Task manager");
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new Taskmanager()).commit();
                    break;

if u r using simple activity then just call :

getSupportActionBar().setTitle("Contact Us");

to change actionbar/toolbar color in activity use :

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#06023b")));

to set gradient to actionbar first create gradient : Example directry created > R.drawable.gradient_contactus

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >

    <gradient
        android:angle="90"
        android:startColor="#2980b9"
        android:centerColor="#6dd5fa"
        android:endColor="#2980b9">
    </gradient>


</shape>

and then set it like this :

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_contactus));

The Easiest way to change the action bar name is to go to the AndroidManifest.xml and type this code.

<activity android:name=".MainActivity"
    android:label="Your Label> </activity>

getActionBar().setTitle("edit your text"); 

You just add this code in the onCreate method.

setTitle("new title");

For future developers that are using AndroidX and the navigation architectural component.

Instead of setting the toolbar title using one of the solutions above, which can be very painful if you want to set it dynamically on a back stack change, you can set a placeholder for the title of the fragment in the navigation graph like the following:

<fragment
    android:id="@+id/some_fragment"
    android:name="package.SomeFragment"
    android:label="Hello {placeholder}"
    tools:layout="@layout/fragment_some">
    <argument
        android:name="placeholder"
        app:argType="string" />
</fragment>

The placeholder value has to be provided using the FragmentDirections (via the action method).

It is then replaced in the title and show like Hello World (when placeholder = "World").


Inside Activity.onCreate() callback or in the another place where you need to change title:

getSupportActionBar().setTitle("Whatever title");

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);


You can define the label for each activity in your manifest file.

A normal definition of a activity looks like this:

<activity
     android:name=".ui.myactivity"
     android:label="@string/Title Text" />

Where title text should be replaced by the id of a string resource for this activity.

You can also set the title text from code if you want to set it dynamically.

setTitle(address.getCity());

with this line the title is set to the city of a specific adress in the oncreate method of my activity.


Kotlin

You can do it programmatically in Kotlin this way. You just ignore it if "supportActionBar" is null:

    supportActionBar?.setTitle(R.string.my_text)
    supportActionBar?.title = "My text"

Or this way. It throws an exception if "supportActionBar" is null.

    supportActionBar!!.setTitle(R.string.my_text)
    supportActionBar!!.title = "My text"

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:

  1. In the Manifest: in the manifest file, set the label of each Activity.

    android:label="@string/TitleWhichYouWantToDisplay"
    
  2. 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);
    
        }
    }
    

Little bit older but had the same problem. I did it like this:

strings.xml

<string name="title_awesome_app">My Awesome App</string>

and make sure you set this in your AndroidManifest.xml:

<activity
            ...
            android:label="@string/title_awesome_app" >
            ...
</activity>

it's easy and you don't have to worry about null-references and other stuff.


In onCreateView add this:

(activity as AppCompatActivity).supportActionBar?.title ="My APP Title"

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);

    }
}

ActionBar ab = getActionBar();
TextView tv = new TextView(getApplicationContext());

LayoutParams lp = new RelativeLayout.LayoutParams(
        LayoutParams.MATCH_PARENT, // Width of TextView
        LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setTextColor(Color.RED);
ab.setCustomView(tv);

For more information check this link :

http://android--code.blogspot.in/2015/09/android-how-to-change-actionbar-title_21.html


try do this...

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
    this.setTitle(String.format(your_format_string, your_personal_text_to_display));
    setContentView(R.layout.your_layout);
       ...
       ...
}

it works for me


getSupportActionBar().setTitle("title");