Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing LinearLayout onClick events with ImageButton inside it

I have an ImageButton and a TextView wrapped in a LinearLayout like this:

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="20" android:gravity="center"
        android:clickable="true" android:id="@+id/action_showhide">
        <ImageButton android:id="@+id/test"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/ic_toggle_hide_states" android:background="@null"></ImageButton>
        <TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/txtHide"
            android:textColor="@drawable/orange" android:textStyle="bold"></TextView>
    </LinearLayout>

The ImageButton is backed by a custom drawable for normal, focused and pressed states. I would like to allow the user to click anywhere in the LinearLayout to fire an OnClick event. The code below shows the set up for the OnClickListener:

    final LinearLayout actionHide = (LinearLayout) findViewById(R.id.action_showhide);
    actionHide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(AppAdvocate.TAG, "Event caught");
        }
    });

The code works when the user clicks anywhere on the TextView but when the user clicks on the ImageButton the event doesn't bubble up to the LinearLayout. I am not defining an onClickListener for the button. I want the drawable for my ImageButton to change so I don't want to set it to clickable=false. Is there a way to bubble up the event?

like image 663
Richard Avatar asked Aug 25 '10 22:08

Richard


1 Answers

In order to make LinearLayout clickable you need to set android:focusable="false" on all its child elements.

like image 54
pedroTavares Avatar answered Nov 07 '22 00:11

pedroTavares