How to Change Text Color of Toolbar Title in an Android App? - GeeksforGeeks

23 Feb, 2021
In an Android app, the toolbar title present at the upper part of the application. Below is a sample image that shows you where the toolbar title is present.
In the above image, you may see that the color of the Toolbar Title is white which is by default. So in this article, you will learn how to change the text color of the Toolbar Title in an Android App. There are two ways to change the color of the Toolbar Title.
In method 1 Just go to the activity_main.xml file and add a TextView in the toolbar widget with the text color attribute. The complete code for the activity_main.xml file is given below.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <androidx.appcompat.widget.Toolbar        android:id="@+id/toolbar"        android:layout_width="match_parent"        android:layout_height="?attr/actionBarSize"        android:background="#0F9D58">          <TextView            android:id="@+id/custom_title"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="GeeksForGeeks"            android:textColor="#D61010"            android:textSize="20sp"            android:textStyle="bold" />      </androidx.appcompat.widget.Toolbar>  </RelativeLayout>
Output UI:
Step 1: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <androidx.appcompat.widget.Toolbar        android:id="@+id/toolbar"        android:layout_width="match_parent"        android:layout_height="?attr/actionBarSize"        android:background="#0F9D58">    </androidx.appcompat.widget.Toolbar>  </RelativeLayout>
Step 2: Changes in the themes.xml file
Go to the app > res > values > themes > themes.xml file and add the following line inside the <resources> tag.
<item name=”windowNoTitle”>true</item>
Step 3: Working with the MainActivity file 
In the activity’s onCreate() method, call the activity’s setSupportActionBar() method, and pass the activity’s toolbar. This method sets the toolbar as the app bar for the activity. Add below codes in your Activity to set the text color to the Toolbar title. Below is the complete code for the MainActivity.java / MainActivity.kt file.  
Java
Kotlin
import android.graphics.Color;import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;import androidx.appcompat.widget.Toolbar;  public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          Toolbar toolbar = findViewById(R.id.toolbar);        toolbar.setTitleTextColor(Color.RED);        setSupportActionBar(toolbar);    }}
import android.graphics.Colorimport android.os.Bundleimport androidx.appcompat.app.AppCompatActivityimport androidx.appcompat.widget.Toolbar  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          val toolbar: Toolbar = findViewById(R.id.toolbar)        toolbar.setTitleTextColor(Color.RED)        setSupportActionBar(toolbar)    }}
Output: 
namanjha10
Android-Bars
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flutter - Custom Bottom Navigation Bar
Retrofit with Kotlin Coroutine in Android
GridView in Android with Example
How to Change the Background Color After Clicking the Button in Android?
Android Listview in Java with Example
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java