Reputation: 12768
Consider:
styles.xml
<style name="BlueTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="theme_color">@color/theme_color_blue</item>
</style>
attrs.xml
<attr name="theme_color" format="reference" />
color.xml
<color name="theme_color_blue">#ff0071d3</color>
and finally the drawable progress_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<stroke
android:width="2px"
android:color="#ff515151" />
<solid android:color="@color/transparent" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="?attr/theme_color" />
</shape>
</clip>
</item>
</layer-list>
when using progress bar drawable:
<ProgressBar
android:id="@+id/progressBarStartup"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:progressDrawable="@drawable/progress_bar"
android:visibility="visible" />
I get a runtime error. It seems Android can't get the color theme.
06-24 16:35:57.032: E/AndroidRuntime(3493): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.xxx.activities.startup.ActivityStartup}: android.view.InflateException: Binary XML file line #33: Error inflating class android.widget.ProgressBar
06-24 16:45:32.265: E/AndroidRuntime(9901): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010000 a=-1}
Upvotes: 4
Views: 2743
Reputation: 10214
Yes, this seems to be a known problem. But there is a workaround. Instead of using ?attr
referenced colors like you described, create several drawables (as many as the different colors you have), let each call its colors directly. On the theme level, reference to the drawables themselves depending on the theme. It works that way.
Update:
On Lollipop, you can tint the drawable in XML. To cover pre-Lollipop, you can simply call this for all your drawables in question (thanks to the newer versions of the appcompat library):
public void setDrawableColor(Drawable d, int color) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
d = DrawableCompat.wrap(d);
DrawableCompat.setTint(d.mutate(), color);
}
}
Upvotes: 3