Qin
Qin

Reputation: 191

Android ImageView selector does not work

This is my selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/white_small_down_arrow_v4"   android:state_pressed="true"/>  
    <item android:drawable="@drawable/white_small_up_arrow_v4" android:state_focused="false"/> 
    <item android:drawable="@drawable/white_small_up_arrow_v4" /> <!-- default -->

</selector>

This is how I applied it on ImageView:

     <ImageView
            android:id="@+id/change_city_small_down_ImageView"
            android:clickable="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/changeCityRelativeLayout"
            android:layout_marginLeft="5dp"
            android:background="@drawable/change_city_selector"
   </ImageView>

Now, the problem is, when I pressed the ImageView, the according state drawable image does not change. I have tried it on other wigdet, also not work. I can't figure out why, becasue I used to do this the same way, and it works.

I have monitored imageview states when it been clicked.

v.hasFocus() : false , v.isClickable() : true , v.isInTouchMode() :true , v.isEnabled() : true , v.isPressed() : true


I made a terrible mistake, the white_small_down_arrow_v4 and white_small_up_arrow_v4 actually pointing the same direction, in other words, they are same picture.

so, probably my mistake will help someone else if they found selector does not work, and first thing to do is to check if the state drawables are the same....

Upvotes: 3

Views: 6405

Answers (6)

Amr
Amr

Reputation: 1312

Make sure that your selector actually has state_selected instead of state_checked the later will not work

your_selector.xml
    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_heart_selected" android:state_selected="true" />
    <item android:drawable="@drawable/ic_heart_unselected" android:state_selected="false" />
</selector>

if you have state_checked instead of state_selected, ImageView will never work.

Upvotes: 0

Elron
Elron

Reputation: 531

Try this, it's checked on Android 4.4.2 and 5.1:

/drawable

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

/layout

<ImageView
    android:id="@+id/ivOpenFile"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_margin="8dp"
    android:layout_alignParentRight="true"
    android:padding="4dp"
    android:background="@drawable/selector_settings_item"
    android:clickable="true"
    android:focusableInTouchMode="true"
    android:visibility="invisible"
    />

/java

ivOpenFile = (ImageView) rootView.findViewById(R.id.ivOpenFile);
ivOpenFile.setImageDrawable(VectorDrawableCompat.create(
        getResources(),
        R.drawable.vd_action_files_black,
        null));

Upvotes: 0

Maher Abuthraa
Maher Abuthraa

Reputation: 17813

You need to set clickable to true in ImageView

android:clickable="true"  

Upvotes: 1

Ashkan
Ashkan

Reputation: 1378

change you selection to this:

<?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/white_small_down_arrow_v4"   android:state_pressed="true"/>  
    <item android:drawable="@drawable/white_small_up_arrow_v4" android:state_focused="true"/> 
    <item android:drawable="@drawable/white_small_up_arrow_v4" /> <!-- default -->
  </selector>

both of them should be true

Upvotes: 0

adnanyousafch
adnanyousafch

Reputation: 1172

Try this: use image android:src="@drawable/change_city_selector" instead of android:background="@drawable/change_city_selector"

<ImageView
        android:id="@+id/change_city_small_down_ImageView"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/changeCityRelativeLayout"
        android:layout_marginLeft="5dp"
        android:src="@drawable/change_city_selector"
</ImageView>

Upvotes: 2

markas
markas

Reputation: 91

Try adding android:focusable="true" and android:focusableintouchmode="true" in your ImageView property.

Upvotes: 1

Related Questions