Android: How to propagate click event to LinearLayout childs and change their drawable
Asked Answered
S

5

28

I would like to create a linear layout which would behave similarly to ImageButton.

<LinearLayout
    android:id="@+id/container"
    style="?WidgetHomeIconContainer">            

    <ImageView
        android:id="@+id/icon"
        style="?WidgetHomeIcon" />

    <TextView
        android:id="@+id/title"
        style="?WidgetHomeLabel"             
        android:text="@string/title"
        android:textAppearance="?attr/TextHomeLabel" />
</LinearLayout>

In styles of ImageView, TextView and LinearLayout, I set a selectors for all states.

Now:

  • when I click on ImageView (I tried it also with ImageButton) - it behaves correctly and the image is changed according the selector xml.
  • when I click on LinearLayout - the linear layout is clicked, but the the ImageView and TextView don't change it's drawable/appearance

So I would like to do the following. When I click on parent LinearLayout, I need to change all it's childs to pressed state.

I tried to add following code to LinearLayout onClickListener to propagate the click:

@Override
public void onClick(View v)
{
    LinearLayout l = (LinearLayout) v;
    for(int i = 0; i < l.getChildCount(); i++)
    {
        l.getChildAt(i).setClickable(true);
        l.getChildAt(i).performClick();
    }
}

But it still reamins the same. Thank you very much for any help.

Schist answered 16/4, 2012 at 8:40 Comment(0)
T
55

Put

android:duplicateParentState="true"

in your ImageView and TextView..then the views get its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.

Tamandua answered 16/4, 2012 at 8:59 Comment(3)
android:duplicateParentState="true" doesn't work for me, any idea why?Sangfroid
I have the same problem but the click works and the selector doesn't work, any idea?Procrastinate
Every time I forget how this is done I always end up here. Thank you, sincerely.Lawley
H
8

Not only make for every child:

android:duplicateParentState="true"

But also additionally:

android:clickable="false"  

This will prevent unexpected behaviour (or solution simply not working) if clickable child views are used.

SO Source

Hustle answered 27/5, 2017 at 18:14 Comment(1)
I also had to add android:focusable="false" for this to work.Peculiarize
A
1

After having the same problem some months later, I found this solution:

private void setOnClickListeners() {
    super.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            onClick(v);
        }
    });
    for (int index = 0; index < super.getChildCount(); index++) {
        View view = super.getChildAt(index);
        view.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                onClick(v);
            }
        });
    }
}

protected void onClick(View v) {
    // something to do here...
}
Arthurarthurian answered 26/11, 2012 at 7:59 Comment(0)
B
1

In my case, no one of the other solutions works!

I finally had to use OnTouchListener as explained here, capturing the event when the user clicks in the parent view, and removing all childs OnClickListener.

So the idea is, delegate the click behavior to the parent, and notify the child that is really clicked, if you want to propagate the event. ¡¡That's what we are looking for!!

Then, we need to check which child has been clicked. You can find a reference here to know how it´s done. But the idea is basiclly getting the area of the child, and asking for who contains the clicked coordinates, to perform his action (or not).

Busse answered 17/5, 2018 at 17:16 Comment(0)
A
1

At first, my child view failed to get click from parent. After investigating, what I need to do to make it work are:

  1. remove click listener on child view
  2. adding click listener on parent view

So, I don't need to add these on every children.

android:duplicateParentState="true"
android:clickable="false"

I only add duplicateParentState to one of my child view.

My child view is now listening to parent click event.

Almeria answered 21/2, 2019 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.