How to get primary color of material theme in xml drawable?
I’m trying to define round drawable which needs primary color of material theme. Here’s my xml code:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <solid android:color="?attr/colorPrimary"></solid> </shape>
But I’m getting
InflateException. I’ve read that we can not use attributes in drawable xml. So any workaround for this ?
3 Solutions collect form web for “How to get primary color of material theme in xml drawable?”
If you are using API 21 or greater, you should use:
For older APIs you should just type:
Is your colorPrimary stored in yours colors.xml? If so, just reference it like @color/colorPrimary.
If not, just define it in colors.xml and reference it. These are the default teal color of meterial straight from the source code. I think material_deep_teal_500 is what you are looking for.
<!-- Primary & accent colors --> <color name="material_deep_teal_200">#ff80cbc4</color> <color name="material_deep_teal_500">#ff009688</color> <color name="material_blue_grey_800">#ff37474f</color> <color name="material_blue_grey_900">#ff263238</color> <color name="material_blue_grey_950">#ff21272b</color>
You can’t use attribute values in drawables.
You would be best to use a semi-transparent white/black (depending on if you are light or dark) like #44FFFFFF and overlaying over the primary color background. You’ll notice most material drawables are a shade lighter/darker over a primary color background…
Otherwise, if you need the color duplicated – you will have to use @color/myPrimaryColor – which should be the same as what you defined in your theme as primaryColor.