22

I try to make a custom Drawable by using attribute but I have an inflating error

01-21 11:01:06.171: E/AndroidRuntime(13695): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/action_bar_background.xml from drawable resource ID #0x7f020000 

My action_bar_background.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="90" android:endColor="?attr/ActionBarEndColor" android:startColor="?attr/ActionBarStartColor" /> </shape> 

My attr.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="ActionBarStartColor" format="reference|color" /> <attr name="ActionBarEndColor" format="reference|color" /> </resources> 

My themes.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.Theme1" parent="@style/Theme.GreenDroid.Light.NoTitleBar"> <item name="ActionBarStartColor">@color/greyDark</item> <item name="ActionBarEndColor">@color/greyLight</item> </style> <style name="Theme.Theme2" parent="@style/Theme.GreenDroid.Light.NoTitleBar"> <item name="ActionBarStartColor">@color/myRed</item> <item name="ActionBarEndColor">@color/myWhite</item> </style> </resources> 

I don't undestand why, my attribute is a reference or a color, if I "force" the color in themes.xml, it don't work too.

Can anybody help?

I've found this question but it's not resolve my problem.

3
  • The only solution i've found, it's to use multiple drawable file with the choosen color and make the reference in the theme file. Commented Jan 23, 2012 at 23:03
  • 1
    This issue was solved in Android L preview, as specified here: code.google.com/p/android/issues/detail?id=26251 Commented Jul 10, 2014 at 6:50
  • I found this fixed in Lollipop, but still doesn't work Kitkat and less Commented Nov 20, 2014 at 21:21

1 Answer 1

13

Good news everyone! Starting Lollipop (21) you can use attributes to reference stuff like colors e.g. ?attr/colorPrimary.

Original answer:

As far I know you cannot reference style attributes from drawables, I couldn't find an usage like that in Android platform resources ($ANDROID_HOME/platforms/android-*/data/res/), all I see is that every drawable even for gradient drawables are defined in place or they just reference other drawables like color drawables.

Here is a related bug report for this (star it):

https://code.google.com/p/android/issues/detail?id=26251

You can workaround this by using separate xml drawables like explained at:

https://stackoverflow.com/a/13471695/172670

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately it will still crash on anything lower than API 21. (So on devices running KitKat or lower it will still crash.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.