1

I'm trying to define a custom color for my app's theme. Here is how I do:

define custom attribute:

<declare-styleable name="ApplicationStyle">
    <attr name="colorWeekdaysBg" format="color"/>
</declare-styleable>

Define an application style:

<style name="ApplicationStyle" parent="Theme.AppCompat.NoActionBar">
    <item name="android:editTextStyle">@style/EditTextStyle</item>
    <item name="colorPrimaryDark">@color/dark_blue</item>
    <item name="colorPrimary">@color/dark_blue</item>
    <item name="colorAccent">@color/dark_blue</item>
    <item name="colorWeekdaysBg">@color/access_weekdays</item>
</style>

Set style in the manifest:

<application
    android:name=".App"
    android:icon="@drawable/icon_launcher"
    android:label="@string/app_name"
    android:theme="@style/ApplicationStyle">

Use this attribute in the drawable xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:right="-1dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="?colorWeekdaysBg" />

            <corners
                android:bottomLeftRadius="100dp"
                android:topLeftRadius="100dp" />

        </shape>
    </item>
</layer-list>

But for some reason it does not apply my color to the drawable. It applies a transparent color instead.

One more strange thing is if I replace my ?colorWeekdaysBg with ?colorAccent, which is defined in the Theme.AppCompat, then it applies correct color.

So finally the question: Do you have any idea why it doesn't work, and how to fix it?

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52

1 Answers1

1

As per answer give by mudit here it is a bug in the pre-lollipop OS.
It is reported in Google's Issue Tracker.

In the devices which have Android Lollipop and above OS version it will work fine.

Check below code:

attr.xml

<declare-styleable name="AppTheme">
        <attr name="colorWeekdaysBg" format="color|reference" /> <!-- Or you can keep color only. It will be ok -->
    </declare-styleable>

You have to set the attribute in the v21 style file. Might be you missed this one.
v21\styles.xml

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:splitMotionEvents">false</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:navigationBarColor">@color/black</item>
        <item name="colorWeekdaysBg">@color/access_weekdays</item>
    </style>

Create two drawable files to get it work on all devices. One for normal i.e. pre-lollipop version and another for Lollipop and above i.e. v21 drawable.

your_drawable.xml In this you have to set color from the color file.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:right="-1dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/access_weekdays" />
            <corners
                android:bottomLeftRadius="100dp"
                android:topLeftRadius="100dp" />
        </shape>
    </item>
</layer-list>

v21\your_drawable.xml In this file you can set custom attribute like you did.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:right="-1dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="?colorWeekdaysBg" />
            <corners
                android:bottomLeftRadius="100dp"
                android:topLeftRadius="100dp" />
        </shape>
    </item>
</layer-list>

your_layout.xml In your layout you can give that drawable as below

<Button
    android:id="@+id/btnTest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/your_drawable"
    android:text="Test Text" />

Now check the answers of your questions.

But for some reason it does not apply my color to the drawable. It applies a transparent color instead.

  • Because you may not have defined your attribute in v21 style.xml file

One more strange thing is if I replace my ?colorWeekdaysBg with ?colorAccent, which is defined in the Theme.AppCompat, then it applies the correct color.

  • Android Studio displays color in preview but when you will run the application and reached on that screen your application will be crash with the Error inflating class error
    android.view.InflateException: Binary XML file line #68: Error inflating class Button

So finally the question: Do you have any idea why it doesn't work, and how to fix it?

  • I think now you have got the idea that why it doesn't work and how to fix it.
Viraj Patel
  • 2,113
  • 16
  • 23