Android ImageButton how to have a clickable area that is larger than the image itself?

You could wrap your ImageButton in another ViewGroup and set padding on the ViewGroup. Then you would listen for clicks on the ViewGroup. Here's an example:

<FrameLayout
    android:id="@+id/button_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp" >

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_button"
        android:duplicateParentState="true"
        android:clickable="false"
        android:focusable="false" />

</FrameLayout>

You would then listen for clicks on R.id.button_layout, and your ImageButton should receive all the same states (pressed, clicked, etc.) as the parent layout.

Tags:

Android