I’ve developed a custom drawable for rendering a stylied field. It has two custom colour fields defined in values/attrs.xml
:
<code><attr format="color" name="fieldColor" />
<attr format="color" name="chalkColor" />
</code>
<code><attr format="color" name="fieldColor" />
<attr format="color" name="chalkColor" />
</code>
<attr format="color" name="fieldColor" />
<attr format="color" name="chalkColor" />
These colours are referenced in values/themes.xml
:
<code><style name="Base.Theme.Game" parent="Theme.Material3.DayNight.NoActionBar">
<item name="fieldColor">@color/lightGreen</item>
<item name="trackColor">@color/black</item>
</code>
<code><style name="Base.Theme.Game" parent="Theme.Material3.DayNight.NoActionBar">
<item name="fieldColor">@color/lightGreen</item>
<item name="trackColor">@color/black</item>
</code>
<style name="Base.Theme.Game" parent="Theme.Material3.DayNight.NoActionBar">
<item name="fieldColor">@color/lightGreen</item>
<item name="trackColor">@color/black</item>
and values-night/themes.xml
:
<code><style name="Base.Theme.Game" parent="Theme.Material3.DayNight.NoActionBar">
<item name="fieldColor">@color/darkGreen</item>
<item name="trackColor">@color/white</item>
</code>
<code><style name="Base.Theme.Game" parent="Theme.Material3.DayNight.NoActionBar">
<item name="fieldColor">@color/darkGreen</item>
<item name="trackColor">@color/white</item>
</code>
<style name="Base.Theme.Game" parent="Theme.Material3.DayNight.NoActionBar">
<item name="fieldColor">@color/darkGreen</item>
<item name="trackColor">@color/white</item>
from colours defined in valuescolors.xml
In my XML drawable file drawable/ic_field.xml
I use the attrubutes thus:
<code><?xml version="1.0" encoding="utf-8"?>
<drawable xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
class="org.dobbo.game.drawable.FieldDrawable"
app:fieldColor="?attr/fieldColor"
app:trackColor="?attr/trackColor"
app:trackWidth="@dimen/field_strokeWidth" />
</code>
<code><?xml version="1.0" encoding="utf-8"?>
<drawable xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
class="org.dobbo.game.drawable.FieldDrawable"
app:fieldColor="?attr/fieldColor"
app:trackColor="?attr/trackColor"
app:trackWidth="@dimen/field_strokeWidth" />
</code>
<?xml version="1.0" encoding="utf-8"?>
<drawable xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
class="org.dobbo.game.drawable.FieldDrawable"
app:fieldColor="?attr/fieldColor"
app:trackColor="?attr/trackColor"
app:trackWidth="@dimen/field_strokeWidth" />
Which correctly shows the colours in the lefthand margin. But at runtime I get the following exception when trying to extract the first attrubute:
<code>Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute
at index 0: TypedValue{t=0x2/d=0x7f0301da a=-1}, theme=null
at android.content.res.TypedArray.getColor(TypedArray.java:529)
at org.dobbo.game.FieldDrawable.inflate(FieldDrawable.java:149)
at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:141)
at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1394)
at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1355)
at android.content.res.ResourcesImpl.loadXmlDrawable(ResourcesImpl.java:945)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:888)
... 94 more
</code>
<code>Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute
at index 0: TypedValue{t=0x2/d=0x7f0301da a=-1}, theme=null
at android.content.res.TypedArray.getColor(TypedArray.java:529)
at org.dobbo.game.FieldDrawable.inflate(FieldDrawable.java:149)
at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:141)
at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1394)
at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1355)
at android.content.res.ResourcesImpl.loadXmlDrawable(ResourcesImpl.java:945)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:888)
... 94 more
</code>
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute
at index 0: TypedValue{t=0x2/d=0x7f0301da a=-1}, theme=null
at android.content.res.TypedArray.getColor(TypedArray.java:529)
at org.dobbo.game.FieldDrawable.inflate(FieldDrawable.java:149)
at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:141)
at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1394)
at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1355)
at android.content.res.ResourcesImpl.loadXmlDrawable(ResourcesImpl.java:945)
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:888)
... 94 more
Where the code looks like this:
<code>@Override
public void inflate(final @NonNull Resources resources,
final @NonNull XmlPullParser parser,
final @NonNull AttributeSet attrs,
final @Nullable Resources.Theme theme)
throws IOException, XmlPullParserException {
super.inflate(resources, parser, attrs, theme);
final TypedArray styling = null != theme
? theme.obtainStyledAttributes(attrs, R.styleable.FieldDrawable, 0, 0)
: resources.obtainAttributes(attrs, R.styleable.FieldDrawable);
try {
setFieldColor(styling.getColor(R.styleable.FieldDrawable_fieldColor, getFieldColor()));
</code>
<code>@Override
public void inflate(final @NonNull Resources resources,
final @NonNull XmlPullParser parser,
final @NonNull AttributeSet attrs,
final @Nullable Resources.Theme theme)
throws IOException, XmlPullParserException {
super.inflate(resources, parser, attrs, theme);
final TypedArray styling = null != theme
? theme.obtainStyledAttributes(attrs, R.styleable.FieldDrawable, 0, 0)
: resources.obtainAttributes(attrs, R.styleable.FieldDrawable);
try {
setFieldColor(styling.getColor(R.styleable.FieldDrawable_fieldColor, getFieldColor()));
</code>
@Override
public void inflate(final @NonNull Resources resources,
final @NonNull XmlPullParser parser,
final @NonNull AttributeSet attrs,
final @Nullable Resources.Theme theme)
throws IOException, XmlPullParserException {
super.inflate(resources, parser, attrs, theme);
final TypedArray styling = null != theme
? theme.obtainStyledAttributes(attrs, R.styleable.FieldDrawable, 0, 0)
: resources.obtainAttributes(attrs, R.styleable.FieldDrawable);
try {
setFieldColor(styling.getColor(R.styleable.FieldDrawable_fieldColor, getFieldColor()));
Many thanks for your help.