I’ve been redesigning the user interface for Echo lately and I ran into a bit of an issue when trying to customize the title bar background. Specifically I could get it to work on older versions of Android but not on the new Holo Themes. Knowing the solution now I can tell you…
It’s a silly issue. I blame myself for having spent much time on it, but as I was unable to find a solution when searching for one on the usual channels I will provide it here.
First things first you need to have a custom theme declared for your application (or activity, depending on your needs). Something like…
<!-- Somewhere in AndroidManifest.xml --> <application ... android:theme="@style/ThemeSelector">
Then, declare your custom theme for two cases, API versions with and without the Holo Themes. For the old themes we’ll customize the
windowTitleBackgroundStyle attribute, and for the newer ones the
<!-- res/values/styles.xml --> <?xml version="1.0" encoding="utf-8"?> <resources> <style name="ThemeSelector" parent="android:Theme.Light"> <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item> </style> <style name="WindowTitleBackground"> <item name="android:background">@color/title_background</item> </style> </resources>
<!-- res/values-v11/styles.xml --> <?xml version="1.0" encoding="utf-8"?> <resources> <style name="ThemeSelector" parent="android:Theme.Holo.Light"> <item name="android:actionBarStyle">@style/ActionBar</item> </style> <style name="ActionBar" parent="android:style/Widget.Holo.ActionBar"> <item name="android:background">@color/title_background</item> </style> </resources>
That’s it! Here we use
@color/title_background as a background. It could also be a drawable, and you can also customize other attributes (I had to set a padding for my
WindowTitleBackground style for instance).