'ImageView src with drawable selector ignores enabled state

When using a state selector as the src for an ImageView, enabled="false" is being ignored.

i.e. this doesn't work properly:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:src="@drawable/state_drawable" >

P.S. : I have an ugly workaround, I'll post it as an answer shortly, if you have something better or an explanation for this behavior, please let me know.



Solution 1:[1]

Try to add the property android:clickable="true"

Solution 2:[2]

Possible workaround: use a TextView with a compound drawable:

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:drawableLeft="@drawable/state_drawable"
     android:enabled="false" />

This seems to work, and pull the right drawable from state_drawable, but not very intuitive.

Solution 3:[3]

I have just add the property of:

android:background="@drawable/image_selector"

and the "android:src" is not necessary to be added.

Now in your example it should look like this:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:background="@drawable/state_drawable" >

Hope it helps...

Solution 4:[4]

For me worked only this:

image.setClickable(false); 

For ImegeView android:enabled="false" for some reason doesn't work.

Solution 5:[5]

Kotlin extension function to enable/disable an ImageView/ImageButton without selector

fun ImageView.setImageViewEnabled(enabled: Boolean) = if(enabled) {
    drawable.clearColorFilter()
    isEnabled = true
} else {
    drawable.colorFilter = PorterDuffColorFilter(Color.GRAY, PorterDuff.Mode.SRC_IN)
    isEnabled = false
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Changwei Yao
Solution 2 marmor
Solution 3 JohnnyJaxs
Solution 4 DeniSHow
Solution 5 Brayan Armando Yaquian Gonzale