How to Change Android Title Bar Color?


This post is to explain how you can change the Default Color of Android App's or a particular Activity,

This can be done both using Style XML code and Programmatically using Java or Kotlin Code.

Example 1: Change Android Activity Title Bar Color using Style XML

  1. Open your activity_ .xml file under /res/layouts
  2. Click on Code
  3. Look for androidx.appcompat.widget.Toolbar
  4. Now look for attribute android:background=
  5. Now change its value to any hex color code you want example: #eeeeee (its better you add this to colors.xml and use reference instead example: @color/gray
  6. Run your App on the emulator or device you should see that the Titlebar/Toolbar color has changed.
Code Snippet:
<androidx.appcompat.widget.Toolbar
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="?attr/actionBarSize"
 android:background="@color/gray"
 app:popupTheme="@style/AppTheme.PopupOverlay" />
Code Snippet: colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name="gray">#eee</color>
</resources>

Example 2: Programatically using Java/Kotlin Code:

Add the below code in your Activity java class in onCreate() method,

ActionBar aBar; 
aBar= getSupportActionBar(); 
ColorDrawable cd = new ColorDrawable(Color.parseColor("#eee")); 
actionBar.setBackgroundDrawable(cd); 
Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap