How do I change the background color of the ActionBar of an ActionBarActivity using XML?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Changing an Android action bar color through XML is mainly a theme and style problem. In older projects that still mention ActionBarActivity, the correct fix is to style the action bar in styles.xml rather than trying to paint it ad hoc from activity code.
Core Sections
Recognize the legacy context first
ActionBarActivity came from the older AppCompat support library and has since been replaced by AppCompatActivity. The styling approach is still worth understanding, because many legacy projects use the same theme structure and the same XML-based fix.
The key idea is simple: the action bar background is controlled by the activity theme, and that theme can point at a specific action-bar style.
Define an action-bar style in XML
Create or update a style that sets the action bar background.
Including both android:background and background helps with compatibility between framework and AppCompat attribute handling in older projects.
Then define the color resource if it does not already exist.
Attach the style through the app theme
The action-bar style does nothing until the activity theme references it.
If the activity uses this theme, the action bar should pick up the configured background color automatically.
You can apply the theme globally in the manifest:
or only on a specific activity if the styling should be local to one screen.
Know when this will not work
This XML approach assumes the screen is actually using an action bar supplied by the theme. If the project has switched to a custom Toolbar inside the layout with a NoActionBar theme, then styling actionBarStyle will not affect that toolbar at all.
In that newer setup, you would instead style the toolbar background directly in layout XML or through a toolbar theme.
That distinction matters because many "action bar color not changing" bugs come from mixing the old and new app-bar models.
Apply a different theme on older and newer code carefully
In a legacy ActionBarActivity project, the theme usually extends Theme.AppCompat.*. If you inherit from a theme without an action bar or accidentally override the bar elsewhere, your actionBarStyle item may appear to do nothing.
A reliable legacy pattern looks like this:
The parent theme is important because Theme.AppCompat.Light.DarkActionBar includes an action bar by design.
Why XML is better than runtime color changes here
You can change colors in code, but XML is usually the better choice for app-bar styling because it keeps presentation rules centralized and reusable. Themes also apply earlier in the activity lifecycle, which avoids flicker and makes configuration more consistent across screens.
For maintainability, theme-based styling is the better default unless the color truly changes dynamically at runtime.
Common Pitfalls
- Styling
actionBarStylewhile the screen actually uses a customToolbarwith aNoActionBartheme will have no visible effect. - Setting only
android:backgroundor onlybackgroundcan create compatibility issues in older AppCompat projects. - Applying the style but forgetting to assign the theme to the activity or application makes it seem like the XML is being ignored.
- Using a theme parent that does not include an action bar prevents the configured style from appearing.
- Treating
ActionBarActivityadvice as if it automatically applies to modern toolbar-based layouts causes a lot of confusion in mixed-age codebases.
Summary
- In legacy AppCompat projects, action bar color is usually controlled through theme XML, not activity code.
- Define an action-bar style and attach it with
actionBarStylein the activity theme. - Make sure the theme actually includes an action bar, such as
Theme.AppCompat.Light.DarkActionBar. - If the app uses a custom
Toolbar, style the toolbar directly instead. - XML-based styling is usually cleaner and more reliable than one-off runtime color changes.

