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();
}
});
}
}
for every child
The
android:duplicateParentState="true"
made my TextView looks like it's disabled, and cannot receive click event.All you need is set the TextView
clickable="false"
. So the click event will dispatch to parent layout, and the TextView still can react to touch event (with ripple effect).I faced the same problem, and all the XML Attributes didn't work. I am not sure if this happens because i programmatically inflate and add the views, but this is how i worked around the problem.
I have a Class which extends LinearLayout, with a TextView and an ImageView. After inflating the layout and getting the views, I assigned the child views a OnClickListener, when pressed, executes the LineaLayout's onClickListner.
I also tried overriding OnTouchListener, but then my child views didn't have the ripple effect, which I needed.
Make Your parent LinearLayout's android:clickable="true"
Make all of the the childview's android:clickable="false"
Under Linearlayout - Remove android:inputType="" from TextView
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 setandroid:inputType
toTextView
inside myViewGroup
which was blockingonClick()
listener (no idea why)I faced the same problem, and all the XML attributes didn't work. I think this happens because I programmatically inflate and add the views. The fix for me was to - also that programatically - set the inflated root view not clickable:
(Yes, I tried to have the
some_layout
not clickable in XML.)