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 com.google.android.material.appbar.AppBarLayout or androidx.appcompat.widget.Toolbar in your App, the code that should be used inside the MainActivity.java is the same. .
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="6dp" android:textSize="20dp" android:text="Hello World! Dynamic ActionBar Demo"/> </LinearLayout>
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 'com.google.android.material:material:1.1.0'
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="http://schemas.android.com/apk/res/android" package="com.example.testing2"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="Testing2" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="Fixed Title"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Step 5: Now we change the Toolbar Title or ActionBar title dynamically inside the "MainActivity.java" 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 androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import java.util.Calendar; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //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. actionBar.setTitle(dynamicTitle); } //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.