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 ?

  • How to deserialise a subclass in Firebase using getValue(Subclass.class)
  • android design considerations: AsyncTask vs Service (IntentService?)
  • Firebase Cloud Messaging (FCM) - Launch Activity when user clicks the notification with extras
  • how to insert %20 in place of space in android
  • Failed to install HelloAndroid.apk on device 'emulator-5554!
  • Getting Android's system preferences without having any context
  • How to delete sample app from android emulator ?
  • This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz
  • Right way to format date with strings like today, yesterday, tomorrow etc
  • Android N Java8 java.time
  • How to run Frege programs on Android?
  • Create a multiline EditText programatically
  • 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:

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

    For older APIs you should just type:

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

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.