Styling a whole layout

android, android-gui, android-layout

Solution

It's not possible to set a style programatically, so I'm creating a theme and this theme is applied to the activities where it's needed.

Create attributes

Add the styleable attributes to `res/values/attrs.xml` like so:

<declare-styleable name="MyTheme">
    <attr name="titleColor" format="color" />
    <attr name="subtitleColor" format="color" />
    <attr name="backgroundDrawable" format="reference" />
</declare-styleable>

`MyTheme` doesn't seem to need to relate to anything. I suspect it's just a namespace for the attributes.

Create style

In `res/values/styles.xml`:

<style name="MyTheme.LightRows">
    <item name="titleColor">@color/black</item>
    <item name="subtitleColor">@color/grey</item>
    <item name="backgroundDrawable">@drawable/cell_bg</item>
</style>

Apply style to activity

In `AndroidManifest.xml`:

<activity android:name=".view.activity.LightActivity" 
          android:theme="@style/MyTheme.LightRows" />

Reference style attributes

In the layout used for the list row (or whatever):

 android:textColor="?attr/titleColor"

Problem

I have a layout that I use for rows in a `ListView`. In some places, I need to show the layout in a 'light' style and in some places 'dark'. How can I accomplish this using Android styles, themes, etc? I understand the concept of styles, but it seems like they're only for a single element. They let me define a set of properties that are applied to the element with the style. What I'd like to do is define the following properties: - Title text color (applied to `TextView` 1) - Subtitle text color (applied to `TextView` 2) - Background (applied to the parent layout)

Original source

Related problems