Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClickListener doesn't work with clickable attribute

So, my problem is that OnClickListener doesn't work when I set android:clickable="true" into my class.

This is MyClass xml code:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:clickable="true">
...
...
</RelativeLayout>

MyClass.java:

public class MyClass extends RelativeLayout implements OnClickListener {
    public MyClass(Context context) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.book_item, this);
        setOnClickListener(this);
    }

    public void onClick(View v) {
        Log.v("TAG", "Hello");
    }
...
...
}

It works fine when I set android:clickable to false. What do I wrong?

like image 639
Scit Avatar asked Aug 07 '11 09:08

Scit


1 Answers

Setting an OnClickListener will automatically set the clickable property to true. The code you are showing is confusing though. My understanding is that your MyClass view is the parent of the RelativeLayout shown in the XML file.

If so, the child RelativeLayout will get the touch events first (since it's clickable) but won't do anything with them since it doesn't have a click listener.

Just remove clickable=true from your XML.

like image 161
Romain Guy Avatar answered Oct 25 '22 05:10

Romain Guy