How to reference style attributes from a drawable?
In my experience it is not possible to reference an attribute in an XML drawable.
In order to make your theme you need to:
- Create one XML drawable per theme.
Include the needed color into you drawable directly with the
@color
tag or #RGB format.
Make an attribute for your drawable in attrs.xml.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Attributes must be lowercase as we want to use them for drawables -->
<attr name="my_drawable" format="reference" />
</resources>
Add your drawable to your theme.xml.
<style name="MyTheme" parent="@android:style/Theme.NoTitleBar">
<item name="my_drawable">@drawable/my_drawable</item>
</style>
Reference your drawable in your layout using your attribute.
<TextView android:background="?my_drawable" />
Use of attr reference in selector StateListDrawable
LayerDrawable flexibility on the use of either a ?attr or a drawable can be misleading. If you use the ?attr, it will try to set a custom theme attribute, but if you use a regular resource it will try to load a drawable entirely. Quoting from the LayerDrawable docs :
May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".
StateListDrawables only expect drawables to be references as such:
@[package:]drawable/filename
Notice how ?attr or type is omitted.
Reference : https://developer.android.com/guide/topics/resources/drawable-resource
How to reference colour attribute in drawable?
You might need to do the following to fix your problem:
1) Define 2 colors for each theme in your colors file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="my_color_dark">#ff33B5E5</color>
<color name="my_color_light">#ff355689</color>
</resources>
2) Create file res/values/attrs.xml with contents:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="my_color" format="reference" />
</resources>
3) Assuming you have 2 themes in your styles.xml (Theme.dark
and Theme.light
) define:
<style name="Theme.dark" parent="@style/Theme.Sherlock">
<item name="my_color">@color/my_color_dark</item>
</style>
<style name="Theme.light" parent="@style/Theme.Sherlock.Light">
<item name="my_color">@color/my_color_light</item>
</style>
4) Use the color in a drawable:
<color android:color="?attr/my_color"/>
Hope this should fix your problem.
Unable to reference a color attribute in a drawable
res\values\attrs.xml
<attr name="holo_blue_dark" format="color"/>
res\values\styles.xml
<style name="AppTheme.Dark" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="holo_blue_dark">@color/your_color</item>
</style>
res\values\colors.xml
<color name="your_color">#FF33B5E5</color>
And make sure you are using the theme AppTheme.Dark where you are using that attribute.
Drawable resource from custom attribute
See EdgarK's answer; it's better. (I can't delete this since it's the accepted answer)
Does this answer your question?
"You can use format="integer", the resource id of the drawable, and AttributeSet.getDrawable(...)."
(From https://stackoverflow.com/a/6108156/413254)
Related Topics
Difference Between /Res and /Assets Directories
How to Get an Event in Android Spinner When the Current Selected Item Is Selected Again
What Proguard Configuration Do I Need for Firebase on Android
How to Set Up Android Emulator Proxy Settings
Remove Underline from Links in Textview - Android
How to Start Android Service on Installation
How To: Define Theme (Style) Item for Custom Widget
Android Appcompat 21 Elevation
How to Convert Coordinates of the Image View to the Coordinates of the Bitmap
Android: How to Detect Double-Tap
Custom Facebook Login Button - Android
Adt Will Not Allow Creation of Android Activity
Pop Up Window Over Android Native Incoming Call Screen Like True Caller Android App
About "_Id" Field in Android SQLite
Notification to Restore a Task Rather Than a Specific Activity