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:
Define one or more themes in themes.xml and set the definitions of
your styles there.Define custom attributes, a.k.a. custom styles, in attrs.xml.
Describe what the values of your custom styles are in styles.xml.
In your layout files, give your views a style attribute, which has a
custom style name as their values.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().To change the theme, you simply need to restart your activity.
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
Android Progressbar UI Custom Layout
Cannot Resolve Method 'Getsupportfragmentmanager ( )' Inside Fragment
How to Extract the Text from the Selected Item on the Listview
Bitmap Is Returning Null from Bitmapfactory.Decodefile(Filename)
Emulator Appearing Offline on M1 MAC After the Last Update of Arm64-V8A
On Android 8.1 API 27 Notification Does Not Display
How to Make an Intent with Multiple Actions
How to Get the Size of a Folder on Sd Card in Android
Send Auto Email Programmatically
Custom Format Edit Text Input Android to Accept Credit Card Number
Avoiding Content Type Issues When Downloading a File via Browser on Android
Android: How to Attach a Temporary, Generated Image to an Email
Get Android Google Analytics Referrer Tag
Android How to Programmatically Hide Launcher Icon
Android Listview Get Selected Item
Glide-4.0.0 Missing Placeholder, Error, Glideapp and Does Not Resolve Its Method Placeholder,Error