tags:

views:

20

answers:

1

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?

A: 

If you really don't want to make the button not clickable, you could just add a listener to the button and perform the same action as the LinearLayout onClick. It would appear to the user as if it was one big button.

Mayra
Thanks Mayra, this works. How should I cascade the LinearLayout onClick event down to the ImageButton so that its drawable changes state when the LinearLayout is pressed?
Richard
I'm not sure what state you are referring to. Does the image button act like a Toggle button, where it shows a clicked state? You should be able to just call setChecked(checkedState) on your toggle button.
Mayra