Anthony
Anthony

Reputation: 35938

Is there a way to widen the click area for ImageButton?

I have a table with each cell having multiple buttons. I'm noticing that since the ImageButtons are small, sometimes the user ends up clicking the cell when they really meant to click the ImageButton.

Is there a way to widen the clickable area of an Imagebutton?

Upvotes: 1

Views: 299

Answers (5)

Igor Morais
Igor Morais

Reputation: 645

You can use TouchDelegate.

Example:

 Rect delegateArea = new Rect();

 button.getHitRect(delegateArea);

 delegateArea.top -= 60000;
 delegateArea.bottom += 60000;
 delegateArea.left -= 60000;
 delegateArea.right += 60000;

 final TouchDelegate expandedArea = new TouchDelegate(delegateArea,
                    button);

 if (View.class.isInstance(button.getParent())) {
          ((View) button.getParent()).setTouchDelegate(expandedArea);
 }

Hope its help.

Upvotes: 1

Libin
Libin

Reputation: 17085

set duplicateParentState to ImageButton , so that when parent area is clicked , the ImageButton will receive the click

android:duplicateParentState="true"

Upvotes: 0

aga
aga

Reputation: 29416

Since padding is a space between view's content and its border, padding is your friend. Increase button's padding it and you notice that clickable area became bigger.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006744

If you do not want to affect the visual bounds, use a TouchDelegate. This is covered in the developer documentation.

Upvotes: 0

SDJMcHattie
SDJMcHattie

Reputation: 1699

Have you tried setting the padding on the ImageButton? This usually means make the contents not touch the sides of the view then just make the view a bit bigger to compensate.

Upvotes: 0

Related Questions