Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Change ActionBar Title Programmatically in Android?

  • Last Updated : 11 Aug, 2021

Whenever we develop an Android application and run it, we often observe that the application comes with an ActionBar with the name of the application in it. This happens by default unless explicitly changed. This text is called the title in the application. One can change the title of the application by making changes to the app_name string present in the values.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

But, if an application has more than one activity, all of these activities will have the same title. Suppose we have MainActivity, SecondActivity, and ThirdActivity. While navigating, one can observe that all of them will have the same title, i.e. name of the application. So through this article, we will show how you can dynamically change the title of the application at runtime. You can apply the same concept if there is more than one activity to distinguish among them.

Step by Step Implementation

Step 1: Create a New Project in Android Studio



To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add EditText and a Button in the layout file. We shall type the desired title in the EditText and click the button which would perform the title change operation.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Type something in this EditText
          to change the ActionBar text -->
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
  
    <!-- Click this button to make changes -->
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Text"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/editText"/>
  
</RelativeLayout>

Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and initializing EditText
        // and Button from the layout file
        val mEditText = findViewById<EditText>(R.id.editText)
        val mButtonText = findViewById<Button>(R.id.button1)
  
  
        // When the button is clicked
        mButtonText.setOnClickListener {
  
            // If the EditText field is not empty,
            // the ActionBar title will change
            // Else, the application will display a Toast message
            if(mEditText.text.isNotEmpty()){
                title = mEditText.text.toString()
            } else {
                Toast.makeText(applicationContext, "No input", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

Output:

You can see that we are able to change the title at runtime.




My Personal Notes arrow_drop_up
Recommended Articles
Page :