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
TextView1) - Subtitle text color (applied to
TextView2) - Background (applied to the parent layout)
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.xmllike so:MyThemedoesn’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:Apply style to activity
In
AndroidManifest.xml:Reference style attributes
In the layout used for the list row (or whatever):