1

I have RelativeLayout with android:clickable="true" to change image color from grey to white when button stay pressed (otherwise if android:clickable="false" it doesn't work):

<?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:clickable="true"   <!-- here clickable = 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.

According to documentation, after setting android:clickable="true" to ImageView I stopped to get onClickListener event.

     contacts = (RelativeLayout) findViewById(R.id.contacts);
 contacts.setOnClickListener(this); 

So I have

  • or set clickable="false" and "state_pressed" doesn't work
  • or set clickable="true" and onClickListener doesn't work (no event received)

What should I do, any workaround?

Thanks,

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225

3 Answers3

2

Try to set onClickListener to your ImageView instead of RelativeLayout.

And this may interest you too:

How to pass the onClick event to its parent on Android?

Community
  • 1
  • 1
Michał Z.
  • 4,119
  • 1
  • 22
  • 32
1

Work well, if you set Listner to your Imageview instead Layout.

RobinHood
  • 10,897
  • 4
  • 48
  • 97
0

Try using ImageButton widget instead of ImageView.

Vipul Narkhede
  • 133
  • 4
  • 8