Hong
Hong

Reputation: 18501

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?

Upvotes: 5

Views: 1399

Answers (2)

Jin Li
Jin Li

Reputation: 31

You need to create the 'selector.xml' in the 'drawable/' folder rather than 'color/' folder.enter image description here

Upvotes: 3

Benedict
Benedict

Reputation: 458

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.

Upvotes: 2

Related Questions