I have my ImageView
defined in XML like this:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_imageView"
android:src="@drawable/ic_photo"
android:background="@drawable/button_state_list_drawable" />
The button_state_list_drawable.xml
is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_pressed_state_drawable"
android:state_pressed="true" />
<item android:drawable="@drawable/button_enabled_state_drawable"
android:state_enabled="true" />
</selector>
The button_pressed_state_drawable.xml
is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size android:width="50dp"
android:height="50dp" />
<padding android:left="25dp"
android:right="25dp"
android:top="25dp"
android:bottom="25dp" />
<stroke android:color="#fff"
android:width="1dp" />
<solid android:color="#1aafd0" />
</shape>
The button_enabled_state_drawable.xml
is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size android:width="50dp"
android:height="50dp" />
<padding android:left="25dp"
android:right="25dp"
android:top="25dp"
android:bottom="25dp" />
<stroke android:color="#fff"
android:width="1dp" />
<solid android:color="@android:color/transparent" />
</shape>
The problem is that when I click/touch the ImageView
, the button_pressed_state_drawable
does not seem to be used, i.e. I can't see the <solid android:color="#1aafd0" />
in action (which is basically the only difference between the two drawables), i.e the background color is still transparent instead of changing to blue. Where am I messing up?
I think your ImageView
is not clickable
.
Try to make it clickable by using ImageView.setClicked(true)
.
or
android:clickable="true"
android:focusable="true"
Also make sure that your image resource
is not covered your background.
This is what i've done which works fine.
<ImageView
android:id="@+id/fragment_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_state_list_drawable"
android:clickable="true"
android:focusable="true"
android:padding="20dp"
android:src="@drawable/ic_launcher"/>