Pages

Friday, August 14, 2015

Use different colours based on the theme

Here is just a quick reference on what is involved when you want to use different colours based on the selected theme in an Android app:
  • In attrs.xml, define an attribute that references an item in a theme. Add a line, i.e.
    name="calculator_button_background" format="reference" />
  • In themes.xml, add the item
    name="calculator_button_background">@color/color_dialog_number_button
  • The colour can be a direct @color or, in this case, can use predefined colour in colors.xml
    name="color_dialog_number_button">#474747
 Then, based on the selected theme, the R.attr.calculator_button_background or ?attr/calculator_button_background will reference the colour defined in themes.xml.

No comments: