I finally figured out how to define a custom default background for every container layout in my application
First, I defined a a custom theme as a style with the app compat background as the parent background which overrides the default window background. Please note that I am using the theme from the Android compatibility library to support pre-honeycomb Android devices
<style name="Theme.MyCustomAppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowBackground">@color/my_custom_window_background</item> <item name="android:colorBackground">@color/my_custom_window_background</item> </style>
I applied the above theme to the whole application in the AndroidManifest
< application android:icon="@drawable/ic_launcher" android:label="@string/app_title" android:theme="@style/Theme.MyCustomTheme"/>
The above steps resulted in every window having my default window background color defined in step 1 above including action bar. I definitely did not want my action bar, edit text, text view etc to be the default window color. So, I needed to to go back to step 1 and override the style of specific UI widgets I did not want to have the default window background
<style name="Theme.MyCustomAppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowBackground">@color/my_custom_window_background</item> <item name="android:colorBackground">@color/my_custom_window_background</item> <item name="android:actionMenuTextColor">@color/customMenuColor</item> <item name="android:textViewStyle">@style/customTextViewStyle</item> <item name="android:editTextStyle">@style/customEditTextStyle</item> <item name="android:buttonStyle">@style/customButtonStyle</item> <item name="android:listViewStyle">@style/customListViewStyle</item> <item name="android:tabWidgetStyle">@style/customTabWidgetStyle</item> <!-- Support library compatibility --> <item name="actionBarStyle">@style/customActionBarStyle</item> <item name="actionMenuTextColor">@color/customMenuColor</item>
The last step was to define the each custom style define above either to have custom styles for them or have then assigned the system default styles (overridden because of custom window background)
For example, the custom action bar style will look like this:
<style name="customActionBarStyle"
parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/light_black</item>
<item name="android:displayOptions">showHome|showTitle</item>
<item name="android:titleTextStyle">@style/customActionBarTitleTextStyle</item>
<!-- Support library compatibility -->
<item name="background">@color/light_black</item>
<item name="displayOptions">showHome|showTitle</item>
<item name="titleTextStyle">@style/customActionBarTitleTextStyle</item>
</style>
- The advantage of this approach is you can define a default theme in a centralized way and don't have to define a background for each layout as it will be injected through the custom theme applied to the entire app.. Any future re-theming of the app will be very easy with this approach
Please refer to the Android Documentation on theming including how to theme the action bar for more information