Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClick not triggered on LinearLayout with child

I've got a custom LinearLayout with a smaller TextView child. I'd like to be able to click the area not covered by the TextView, so I set clickable=true and an onclicklistener to the LinearLayout, but onClick is not triggered. If I set the onclick listener on the TextView it works as expected...

Anybody can help?

ar_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ar_item" android:layout_width="202dp"
    android:layout_height="62dp" android:background="@drawable/bg_item_ar"
    android:clickable="true">

    <TextView android:id="@+id/ar_item_txt"
        android:layout_width="164dp" android:layout_height="fill_parent"
        android:paddingBottom="8dp" android:paddingLeft="8dp"
        android:paddingTop="8dp" android:paddingRight="6dp" android:gravity="center"
        android:background="#50000000" />

</LinearLayout>

My custom LinearLayout

public class ARView extends LinearLayout
{    

    public ARView(final Context context, String name, String id)
    {        
        super(context);  
        getLayoutInflater().inflate(R.layout.ar_item, this ,true);
        LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        inflater.inflate(R.layout.ar_item, null);

        TextView textView = (TextView) findViewById(R.id.ar_item_txt);
        textView.setText(name);

        setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {                   
                Toast t = Toast.makeText(context, "hey!", Toast.LENGTH_SHORT);
                t.show();
            }
        });
    }
}
like image 885
jul Avatar asked Jul 20 '11 14:07

jul


3 Answers

android:duplicateParentState="true" did not help me.

To make your layout clickable with its children you need add this option for every child:

 android:clickable="false"

Then click handling will go up to parent.

like image 135
Yurets Avatar answered Nov 03 '22 11:11

Yurets


for every child

android:duplicateParentState="true"

like image 38
Blackbelt Avatar answered Nov 03 '22 10:11

Blackbelt


This isn't your case, but I had similar problem with clickable ViewGroup. After a hour of looking for solution a found out that I set android:inputType to TextView inside my ViewGroup which was blocking onClick() listener (no idea why)

Don't use android:inputType with TextView

like image 24
elementstyle Avatar answered Nov 03 '22 09:11

elementstyle