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); 
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency