changing the ActionBar title programmatically in Android Studio

How To Change The ActionBar Title Programmatically In Android Studio

When switching between Fragments or maybe some other kind of works, you may need to change the ActionBar title according to fragments or user actions. But it’s mostly used while switching between the Fragments. To do so, we need to set it programmatically from the ACTIVITY.java file using a few bunches of code. In this article, we will show you how to do so.

Here’s An Example Of Changing The ActionBar Programmatically in Android Studio:

image

Before we get started we need These:

  • Android Studio
  • An existing App project with an Activity and maybe some Fragments

So, we are all set. Let’s Get Started!

Step 1 – Getting/Retrieving The ActionBar:

Before we can change the ActionBar title, we will need to retrieve it. But first of all, we need to import class for the ActionBar,
If you are using Androidx Library, then import this class:

import androidx.appcompat.app.AppCompatActivity;

And If you are not using Androidx Library, Then import this class:

import android.app.ActionBar;

Now, we will retrieve the ActionBar from our activity, but there are two different lines for different Libraries.
If you are using Androidx Appcompact, then you will need to use this line:

androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();

And if not using Androidx Library, then use this:

ActionBar actionBar = getActionBar();

[If the Activity doesn’t have an ActionBar then it will return null]

Now we can change the title of the ActionBar whenever we need it.

Step 2 – Changing The Title Of The ActionBar/Toolbar Programmatically:

So, to change the ActionBar title, we will need to use this line only:

actionBar.setTitle("YOUR ACTIONBAR's NEW TITLE HERE");

To change the ActionBar title when switching between Fragments, you will need to use this line every time whenever the user switches between the Fragments or you can also use other methods.

So, Our Final Codes Will Look Like This:
With Androidx Library:

package com.zealtyro.example;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("YOUR ACTIONBAR's NEW TITLE HERE");


}
}

Without Androidx Library:

package com.zealtyro.example;

import android.app.ActionBar;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActionBar actionBar = getActionBar();
actionBar.setTitle("YOUR ACTIONBAR's NEW TITLE HERE");


}
}

Congratulations!

You have successfully changed the ActionBar title programmatically.
Thanks For Allowing Us To Help You 🙂
If you are confused or want to know something, then let us know in the comment box, we will reach you as soon as possible. Don’t Forget To Subscribe our Newsletter, YouTube Channel, and Like Our Facebook Page To Keep Updated With Awesome Things. Follow us on Twitter to stay updated with the latest news & changes.

Did you like it?