Question

I am creating a default theme for our Android apps to specify default custom theme to be applied to the whole app. The idea is we shouldn't have to specify appearance related attributes in the layout and they should be automatically injected by applying the custom theme to the entire app.

I am able to do this for UI widgets like TextView and EditText by overriding their styles. For example overriding android:textViewStyle and android:buttonStyle, etc. How can I do the same for a layout (LinearLayout or RelativeLayout, etc) so that I can specify a default background for a layout?

The application element in android manifest looks like this:

....
<application android:icon="@drawable/ic_launcher" android:label="@string/app_title"      android:allowBackup="false" 
    android:theme="@style/Theme.MyCustomTheme" android:name="MyAppClass">

Theme.MyCustomTheme looks like this:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="Theme.MyCustomTheme" parent="Theme.AppCompat">
  <item name="android:textViewStyle">@style/my_custom_text_view_style</item>
  <item name="android:editTextStyle">@style/my_custom_edit_text_style</item>
  <item name="android:buttonStyle">@style/my_custom_button_style</item>
  <item name="android:listViewStyle">@style/my_custom_list_style</item>

The custom styles above inherit from the respective android base styles.

I am looking for the right android attribute to use above to override the style of a layout so that I can apply my custom default background to every layout (LinearLayout or RelativeLayout, etc) declared in the layout xml file without having to explicitly specify it in the layout xml. I tried overriding android:colorBackground but that didn't make any difference. I tried overriding android:windowBackground but that changes the color of the action bar as well. Please note that I am using the appcompat theme from the appcompat support library.

Was it helpful?

Solution

I finally figured out how to define a custom default background for every container layout in my application

  1. 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>
    
  2. 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"/>

  3. 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>
    

  4. 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> 
  1. 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

OTHER TIPS

Looking at themes.xml i didn't see anything applicable to layouts and ViewGroups in general, (although there are styles support for lists and some widgets as you can see).

Therefore I am not sure that it is possible to have your custom style applied to all layouts.

I guess you would still have to apply your custom layout style, (where you could change background color as

<item name="android:background">#ff0000</item> 

) to each of your layouts yourself.

Feel free to prove me wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top