15

I have RelativeLayout like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/contacts"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="0.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:contentDescription="@string/content_description_contacts"
    android:scaleType="fitXY"
    android:src="@drawable/contacts" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignBottom="@id/image"
    android:paddingBottom="10dp"
    android:textColor="@drawable/text_color"
    android:text="@string/button_contacts"
    android:textSize="12sp" />    
</RelativeLayout>

enter image description here

and seems like:

enter image description here

My contacts Selector seems:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
    android:drawable="@drawable/contacts_over" />
<item android:state_selected="true"
    android:drawable="@drawable/contacts_selected" />
<item
     android:drawable="@drawable/contacts_default" />
</selector>

As you can see I have 3 images: by default, selected and pressed.

But I have a problem: only default and state_selected images are work as expected, but state_pressed dosn't seem to work.

I have several above mentioned RelativeLayouts and no one works with state_pressed.

Does anybody know where is my problem?

Thanks!

Onik
  • 19,396
  • 14
  • 68
  • 91
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225

5 Answers5

38

make sure your RelativeLayout is clickable

pskink
  • 23,874
  • 6
  • 66
  • 77
6

You could also set android:addStatesFromChildren="true" in your RelativeLayout instead of the android:clickeable="true". If your children are already clickeable, focusable, etc. You shouldn't make your RelativeLayout clickeable or focusable.

4gus71n
  • 3,717
  • 3
  • 39
  • 66
  • You are right, if you have child views which have already registered Click Listeners then you need to add addStatesFromChildren="true" along with clickable="true" to get the selector working on the Parent Layout. – iAviatorJose Apr 15 '16 at 06:53
5

Try to add to your ImageView android:clickable="true"

Nickolai Astashonok
  • 2,878
  • 3
  • 20
  • 23
2

In my opinion you should use Button and create selector for it instead of making custom button by creating RelativeLayout and putting there ImageView and TextView. Then you can use android:drawableTop="@drawable/your_contact_icon". Afterwards you can check if your selector works fine.

mmBs
  • 8,421
  • 6
  • 38
  • 46
1

hope these helps someone;

  1. make sure view clickable. descendants can block click event. more info search the below attributes.

android:clickable, android:descendantFocusability, android:focusable, android:focusableInTouchMode

  1. in style xml you should define item element state attributes or make sure the item with no attribute must be at the end.

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/lightGold"/>
            <stroke android:width="1dp" android:color="@color/lightGrey"/>
            <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@color/white_two"/>
            <stroke android:width="1dp" android:color="@color/lightGrey"/>
            <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
        </shape>
    </item>
    

But this sample will not work;

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <solid android:color="@color/white_two"/>
        <stroke android:width="1dp" android:color="@color/lightGrey"/>
        <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
    </shape>
</item>
<item android:state_pressed="true">
    <shape>
        <solid android:color="@color/lightGold"/>
        <stroke android:width="1dp" android:color="@color/lightGrey"/>
        <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
    </shape>
</item>

or you can define state attribute for both item. So order will be not important.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape>
            <solid android:color="@color/white_two"/>
            <stroke android:width="1dp" android:color="@color/lightGrey"/>
            <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/lightGold"/>
            <stroke android:width="1dp" android:color="@color/lightGrey"/>
            <padding android:bottom="@dimen/padding1" android:left="@dimen/padding1" android:right="@dimen/padding1" android:top="@dimen/padding1"/>
        </shape>
    </item>
</selector>

Ref;

During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state

State List https://developer.android.com/guide/topics/resources/drawable-resource.html

kaya
  • 724
  • 10
  • 24
  • Thanks for explaining how 'android:state_pressed="false"' makes order not important. – XMAN Dec 15 '18 at 03:28