How to Change Android Title Bar or Toolbar or ActionBar Text Programmatically in Android Studio - Tutorial

Android Dynamic actionbar or toolbar title changing

Android Title Bar or ActionBar or Toolbar is the header of any screen in an App. We usually keep fixed title names to every Activity. Some times, it is necessary to change the title-text dynamically at runtime inside the Java code. Let us change the toolbar-text programmatically.

You can also check Android GridLayout with equal-width columns for easy implementation.

Change Android Title Bar or Toolbar or Action-Bar text Programmatically

Follow the below steps to create a Toolbar and change its title at runtime. 

Step 1: Create a new Android Project using the "Empty Activity" Template.

Step 2: Add the below code to the "activity_main.xml" file manually. Even if you use or androidx.appcompat.widget.Toolbar in your App, the code that should be used inside the is the same. . 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

        android:text="Hello World! Dynamic ActionBar Demo"/>


Step 3: Add the below dependencies to the "build.gradle" Module-level file. Notice that we have upgraded our code to be compatible with AndroidX library.

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation ''

Step 4: Add the below XML code to "AndroidManifest.xml" file. You can add a Label to each activity. These labels are nothing but Fixed or Static title texts.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

        <activity android:name=".MainActivity" android:label="Fixed Title">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Step 5: Now we change the Toolbar Title or ActionBar title dynamically inside the "" file. In this example, we try to set the title to display the actual time at the time of running the app.

package com.example.testing2;

import android.os.Bundle;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        //Start of dynamic title code---------------------
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null)
            Calendar cal = Calendar.getInstance();
            String dynamicTitle = cal.getTime().toString();
            //Setting a dynamic title at runtime. Here, it displays the current time.
        //End of dynamic title code----------------------


It is a good practice to call for getSupportActionBar() instead of getActionBar(). Also, we have to check the ActionBar variable against the null value before accessing its methods and properties.

This is how we can easily change the title of any ActionBar or Toolbar using Android Studio programmatically.

Learn Java basics before diving into Android for fast coding and development.

It is time to share this Android Studio tutorial with your friends and colleagues.



Show some care. Like or Subscribe.

C MCQ App by ExamTray (Give us 5*)

EXAMTRAY C MCQ android app link

Java MCQ App by ExamTray (Give us 5*)

EXAMTRAY Java MCQ android app link
ALL MCQ App by ExamTray (Give us 5*) EXAMTRAY android app link
WUnsaved Whatsapp msg to Unknown(Give us 5*) WUnsaved android app link
Virtual Tape GPS (Give us 5*) Virtual Tape GPS android app link