Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 746
jimmy0251 Avatar asked Oct 23 '14 18:10

jimmy0251


People also ask

How do I change the color of a drawable in xml?

Use app:drawableTint="@color/yourColor" in the xml instade android:drawableTint="@color/yourColor" .

What is colorPrimary and colorPrimaryVariant?

colorPrimary and colorSecondary represent the colors of your brand. colorPrimaryVariant and colorSecondaryVariant are lighter or darker shades of your brand colors. colorSurface is used for “sheets” of material (like cards and bottom sheets) android:colorBackground is the window background color of your app.

What is the location of the color xml file in the android project?

xml" file located at res\values\colors.


2 Answers

If you are using API 21 or greater, you should use:

<solid android:color="?android:colorPrimary"></solid>

For older APIs you should just type:

<solid android:color="?colorPrimary"></solid>
like image 84
Johniak Avatar answered Oct 17 '22 22:10

Johniak


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>
like image 2
opt05 Avatar answered Oct 17 '22 21:10

opt05