6

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?

TN888
  • 7,659
  • 9
  • 48
  • 84
Richard
  • 28,691
  • 8
  • 33
  • 34

4 Answers4

4

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

Jules
  • 14,200
  • 13
  • 56
  • 101
0

from what I understand, you'll be needing the following:

    <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"
android:onClick="viewClicked"></TextView>
    </LinearLayout>

After that, you create the function:

public void viewClicked(View v){

    switch (v.getId())
    {
        case R.id.action_showhide:
            //do something
            break;
        case R.id.action_showhide:
            //do something
            break;
    }

}
g00dy
  • 6,752
  • 2
  • 30
  • 43
0

Set android::clickable="false" on ImageButton. This should help.

vabian
  • 11
0

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.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • 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 Aug 26 '10 at 13:27
  • 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. – Cheryl Simon Aug 26 '10 at 16:09