Search code examples
android-selector

Error: 'color' attribute should be defined


A color selector is defined as following:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <solid android:color="@color/gray" />

        </shape>
    </item>
    <item android:state_focused="true">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <solid android:color="#66666666" />

        </shape>
    </item>
    <item>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <solid android:color="@color/translucent_icon_background" />
        </shape>
    </item>
</selector>

In Android Studio, 'item" is marked red indicating an error as shown by the following screenshot: enter image description here

The app works fine per tests. I am asking because I am afraid it may not be fine on some devices due to that error. Could anyone shed some light on this error? More specifically, can it be ignored?


Solution

  • I had the same error and I removed it by adding a color attribute with the item tag.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:color="#ffffff">  <--**add this** 
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <solid android:color="@color/gray" />
    
        </shape>
    </item>
    <item android:state_focused="true" android:color="#ffffff"> **<--add this** 
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <solid android:color="#66666666" />
    
        </shape>
    </item>
    <item android:color="#ffffff">   **<--add this** 
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <solid android:color="@color/translucent_icon_background" />
        </shape>
    </item>
    

    Good luck.