user2361174
user2361174

Reputation: 1932

How to reduce the click area of an Button?

I have a gridview that is filled with buttons that looks like this:

right now if I click anywhere on the button, it will be pressed. But I want to the button to only be pressed when it's clicked in the light gray area, not the dark gray or white area on the top and left sides. I tried using setPadding(int,int,int,int) and setPaddingRelative(int,int,int,int) but neither of them makes any effect. How do I fix this?

Edit: the button is 50x50 and is already extended from the Button Class

Upvotes: 1

Views: 540

Answers (2)

nbumakov
nbumakov

Reputation: 140

Override onTouchEvent in your custom button:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (isInGrayArea(event.getX(), event.getY())) {
       return super.onTouchEvent(event);
    }
    return false;
}

event.getX() and event.getY() is pixels coordinates, you will need to convert them into dp, I suppose.

Upvotes: 0

Mou
Mou

Reputation: 2137

Make a custom Button view inherit from Button class and override onDraw method with custom code to do that.

You can override touch methods to detect when button is pressed and get the x and y coords to remove cases when button must not be pressed.

Upvotes: 1

Related Questions