Search code examples
androidcolorsimageviewselectortint

Need to change the color of an icon. tint does not work


This is my imageview:

  <ImageView
                android:id="@+id/action_add_iv"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_centerVertical="true"
                android:layout_marginRight="12dp"
                android:tint="@color/gray32"
                android:background="@drawable/plus_selector"
                android:text="0" />

This is my selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <bitmap android:alpha="160" android:src="@drawable/btn_plus_green" android:tint="@color/gray32" />
</item>
<item android:drawable="@drawable/btn_plus_green" android:state_pressed="false" android:tint="@color/gray32" />
</selector>

Now if I press on the icon, it becomes gray, like I want. But if it's not pressed, it shows green. Why doesn't the tint also apply for state_pressed=false?


Solution

  • Change it to:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <bitmap android:alpha="160" android:src="@drawable/btn_plus_green" android:tint="@color/gray32" />
        </item>
        <item android:state_pressed="false">
            <bitmap android:src="@drawable/btn_plus_green" android:tint="@color/gray32" />
        </item>
    </selector>