Android Themes - Defining Colours in Custom Themes

android themes - defining colours in custom themes

I found a solution which seems to work. First you need to define the custom color fields in attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="titleColor" format="reference|color" />
<attr name="introColor" format="reference|color" />

</resources>

Next you define your themes

<style name="AppTheme.MyDark" parent="android:Theme">
<item name="titleColor">#FFFFFF</item>
<item name="introColor">#FFFFFF</item>
</style>

<style name="AppTheme.MyLight" parent="android:Theme">
<item name="titleColor">#000000</item>
<item name="introColor">#004444</item>
</style>

and finally in your layout

<TextView
android:id="@+id/quoteTitle"
android:textColor="?titleColor"
...
</TextView>

<TextView
android:id="@+id/quoteIntro"
android:textColor="?introColor"
...
</TextView>

i found the solution mainly here

There seems to be no explanation in the official android documentation about using attributes. Best resource I found is here

Custom themes and colors in android

Ok, after searching a bit I found a solution like this. Designer doesn't want to use colorPrimary etc, so I add my custom attribute in attrs like this :

<attr name="colorExperiment" format="color"/>

After this I add this attr in styles in theme like this:

 <style name="DarkTheme" parent="AppTheme.NoActionBar">
<item name="android:windowAnimationStyle">@null</item>
<item name="android:windowBackground">@color/onyx</item>
<item name="colorExperiment">@color/onyx</item>
</style>

And use in layout for example:

<TextView
android:id="@+id/auth_caption"
android:includeFontPadding="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="34dp"
android:text="@string/auth_caption"
android:textColor="?colorExperiment"
/>

android: How can I define custom colors, drawables, etc. in themes?

To work with themes and styles in Android you have to:

  1. Define one or more themes in themes.xml and set the definitions of
    your styles there.

  2. Define custom attributes, a.k.a. custom styles, in attrs.xml.

  3. Describe what the values of your custom styles are in styles.xml.

  4. In your layout files, give your views a style attribute, which has a
    custom style name as their values.

  5. Set the theme of your application or activity in either
    AndroidManifest.xml or in the Activity's onCreate(). This is done by calling setTheme() in the activity's onCreate() method, before any call to setContentView().

  6. To change the theme, you simply need to restart your activity.

Sample Image

Iadvice you to look at this tutorial it deals with all that a programmer want to work on android themes (text color, text formatting, state list drawable etc ...)

Change text color based on theme in a selector with different states

Using a theme attribute inside a color selector XML file is only supported in the most recent versions of Android. To overcome this limitation you need to create one color selector file for each theme, and fill them with plain colors. Then create a theme attribute which points to the correct color selector depending on the theme.

source: https://plus.google.com/102404231349657584821/posts/XEeehfwanGy

edit: tested and it works flawlessly!



Related Topics



Leave a reply



Submit