onClick not triggered on LinearLayout with child

android-linearlayout not clickable
android view onclicklistener not working
how to make linearlayout clickable in android
android make layout not clickable

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

android:duplicateParentState="true"

ViewGroup (LinearLayout) and Child onClick event not passed to , Having finally understood what you are asking I think this will fix it. The SeekBar is not focusable and the Button is. This is the reason it will not work for you  Then click handling will go up to parent. 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.

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.

onClick not triggered on LinearLayout with 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. I followed these questions (onClick not triggered on LinearLayout with child, How to set OnClickListener in ViewPager ) but they did not help me. This is my actual code: This is my actual code: MyPageFragment.xml

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

How to Trigger Click Event on LinearLayout with Children in Android, 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  If you run this application, and click on the second editText and then back on the first one, it will not trigger the onClick. You can keep selecting back and forth and it will not trigger the onClick at all.

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

Onclick not working on parent cardview · Issue #12 · jianzhongli , In this android programming source code example, we are going to explore how to trigger click event on LinearLayout with children in android. Below is a step AlertDialog alertDialog = new AlertDialog.Builder(LinearLayoutActivity.this).create(); alertDialog.setMessage("You touched the Linear Layout."); alertDialog.show(); // Return false, then android os will still process click event, // if return true, the on click listener will never be triggered.

Your TextView height covers the whole parent (whole layout) so you might clicking on empty space but not on the layout. Try using wrap_content for android:layout_height for your TextView. Set click listener for the layout as well.

onTouchEvent only working for one view within activity, Parent cardview is not taking onclick event. if MathView is replaced with plain TextView, then it is working fine. I am registering onclick as cvAns  Therefore you could easily set an onclicklistener on any or all of the children of the viewpager if you wanted to capture clicks and potentially save yourself a lot of work. Make sure that one of the unintended views inside the page is not consuming click event.

Handling click and touch events in Android - Tutorial, It does not work because the buttons are Views too, just as LinearLayout and since they are on top they need their own "onTouchEvent" listener, which is onClick  A vertical LinearLayout will only have one child per row (so it is a column of single elements), and a horizontal LinearLayout will only have one single row of elements on the screen. android:layout_weight attribute depicts the importance of the element.

Input events overview, These methods are called by the Android framework when the respective Notice that the onClick() callback in the above example has no return value, onInterceptTouchEvent(MotionEvent) - This allows a ViewGroup to watch events as they are dispatched to child Views. <LinearLayout android:orientation="​vertical" . In child onclick event you can run the parent onclick logic first and then set some flag so that on subsequest call of parent onclick you can skip logic and reset the flag. This when when you will click on parent container your logic will execute as it is but if you click on child first parent logic will be executed then child.

How to set OnClickListener for ImageView in Kotlin Android, Notice that the onClick() callback in the above example has no return value, but some other event listener methods must  \$\begingroup\$ You are correct, the onClick for the individual buttons works, but not the onTouch in the LinearLayout. onTouch on LinearLayout is triggered when clicking around the buttons though. Would it be correct to accept this as the answer, coz this is the solution that works in this case. \$\endgroup\$ – monologish Apr 26 '13 at 6:18

Comments
  • I'm having the exact same issue, did you find a solution?
  • Thanks for this, solved my similar problem of having multiple controls that I want to express a "clicked" visual state, while only having to handle the event in the parent.
  • Although I do still see one very strange issue - when I have clickable = true, width/height=wrap/match, it works, but if I put those into a style and use that style, it doesn't (no visual 'clicked' state, no handler fired). This is on 2.3; any idea why that might be occurring?
  • Thanks for this! Solved an issue I was having today with nested linear layout selection.
  • Sill need to set every child android:clickable="false"
  • This doesnt work always .... refer this ... stackoverflow.com/a/31578111/5886712... or apply below this to child > android:clickable="false"
  • Thank you @Yurets it's work like magic. this must be acceptable answer
  • Thanks. Worked for me too!
  • Yep - worked. My problem was my children were buttons (inherently clickable), so I had to change them to ImageViews. I don't know why I had them as buttons in the first place.
  • thank you! I couldn't figure out why my TextView was intercepting clicks.
  • This was my issue as well, make sure you don't have inputType or have it set to none! Thanks for posting this
  • well nothing worked for me, unless i came across your answer, there was input type for my textview.
  • The width of the TextView is smaller than its parents, so there should remain a clickable area on the right of the Linearlayout.