onClick not triggered on LinearLayout with child
Asked Answered
B

15

78

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();
            }
        });
    }
}
Belgrade answered 20/7, 2011 at 14:52 Comment(0)
E
67

for every child

android:duplicateParentState="true"

Ellamaeellan answered 8/11, 2011 at 12:22 Comment(7)
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.Alveolate
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?Alveolate
Thanks for this! Solved an issue I was having today with nested linear layout selection.Threatt
@Ellamaeellan Here in this scenario where i should put this android:duplicateParentState="true" ? inside Linear Layout or Text View.Restorative
@jaydroider on the LL's children, so TextView I supposeEllamaeellan
Sill need to set every child android:clickable="false"Protagonist
This doesnt work always .... refer this ... https://mcmap.net/q/176198/-onclick-not-triggered-on-linearlayout-with-child... or apply below this to child > android:clickable="false"Brittnybritton
M
123

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.

Minorca answered 23/7, 2015 at 4:22 Comment(5)
Thank you @Minorca it's work like magic. this must be acceptable answerProtagonist
Thanks. Worked for me too!Strategist
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.Swash
What if have to have clickable children within layout???Parshall
then you skip setting android:clickable="false" for those children. @ParshallMinorca
E
67

for every child

android:duplicateParentState="true"

Ellamaeellan answered 8/11, 2011 at 12:22 Comment(7)
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.Alveolate
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?Alveolate
Thanks for this! Solved an issue I was having today with nested linear layout selection.Threatt
@Ellamaeellan Here in this scenario where i should put this android:duplicateParentState="true" ? inside Linear Layout or Text View.Restorative
@jaydroider on the LL's children, so TextView I supposeEllamaeellan
Sill need to set every child android:clickable="false"Protagonist
This doesnt work always .... refer this ... https://mcmap.net/q/176198/-onclick-not-triggered-on-linearlayout-with-child... or apply below this to child > android:clickable="false"Brittnybritton
P
25

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

Pankey answered 16/7, 2016 at 17:40 Comment(3)
thank you! I couldn't figure out why my TextView was intercepting clicks.Wilda
This was my issue as well, make sure you don't have inputType or have it set to none! Thanks for posting thisArchipenko
well nothing worked for me, unless i came across your answer, there was input type for my textview.Livable
A
10

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

Akbar answered 10/12, 2017 at 13:15 Comment(0)
A
5

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).

Aintab answered 23/7, 2017 at 16:57 Comment(0)
V
2

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.

Vile answered 20/7, 2011 at 15:0 Comment(1)
The width of the TextView is smaller than its parents, so there should remain a clickable area on the right of the Linearlayout.Belgrade
I
2

You aren't using your custom View; you're using a standard LinearLayout. Your XML tag should be:

<com.yourcode.ARView ...> ... </com.yourcode.ARView>
Ignoble answered 20/7, 2011 at 17:28 Comment(0)
A
1

One thing to make sure of is that another view is not on top of the view you are trying to click. This is especially common in FrameLayouts (where your sub LinearLayout may be covered) or with Relative Layouts you might have forgot to update this line:

android:layout_below="@id/shouldBeTheIdOfTheViewCurrentlyBeingCovered"

so that views don't fill the same space.

Amberlyamberoid answered 25/9, 2014 at 7:4 Comment(0)
C
1

If the views in question are TextViews, you may need to set them as focusable="false" so that the first click isn't used focusing on the text view.

Cecilycecity answered 26/1, 2017 at 20:27 Comment(0)
F
0

The problem may be from the textview that has android:layout_height="fill_parent" in its layout. If that doesn't fix the issue, the problem may be the onClick() event. The linear layout may not actually ever call onClick() since its a layout. Try overriding the onTouch() event listener instead.

Facile answered 20/7, 2011 at 19:20 Comment(0)
D
0

Add the following attributes to the linearlayout Any Click events not handled by the child views will be automatically passed over to the LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:focusable="true"
    android:clickable="true">

<child/>
<child/>

</LinearLayout>
Disunite answered 6/7, 2017 at 11:58 Comment(0)
O
0

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.

public class MyView extends LinearLayout {
private OnClickListener clickListener;
ImageView imageView;
TextView textView;

@Override
public void setOnClickListener(@Nullable OnClickListener l) {
    super.setOnClickListener(l);
    clickListener = l;
}

void afterViews() {
    imageView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            clickListener.onClick(MyView.this);
            return false;
        }
    });
    textView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            clickListener.onClick(MyView.this);
            return false;
        }
    });
}

I also tried overriding OnTouchListener, but then my child views didn't have the ripple effect, which I needed.

Overword answered 8/9, 2017 at 7:44 Comment(0)
M
0

None of the solutions above forked for me. Than i noticed that a LinkMovementMethod set to TextView inside my LinearLayout. This class intercepts touch events, click events and properties of TextView such as clickable, focusable vs..

Microminiaturization answered 1/12, 2017 at 11:12 Comment(0)
H
0

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:

View view = layoutInflater.inflate(R.layout.some_layout, someLinearLayout, false);
view.setClickable(false);

(Yes, I tried to have the some_layout not clickable in XML.)

Hoberthobey answered 14/9, 2018 at 8:34 Comment(0)
B
0

android:duplicateParentState="true" >= not worked

focusable="false" >= not worked

clickable="false>= not worked

So i did a quick fix by adding Transparent Button on top with match_parent for height and width

Problem solved without any extra code,

Buchenwald answered 18/6, 2022 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.