How to increase hit area of Android button without scaling background?
I have a button with a custom drawable.
<Button
android:layout_width="22dip"
android:layout_height="23dip"
android:background="@drawable/triangle" />
The drawable is a triangle with transparent background.
|\
| \
|__\
I find this button hard to tap. First, it's relatively small. Second, the transparent pixels are not tappable. I would like to keep the drawable the same size, but make the hit area a square shape twice the size of the triangle.
_____________
| |
| |\ |
| | \ |
| |__\ |
|____________|
you can use TouchDelegate API.
final View parent = (View) button.getParent(); // button: the view you want to enlarge hit area
parent.post( new Runnable() {
public void run() {
final Rect rect = new Rect();
button.getHitRect(rect);
rect.top -= 100; // increase top hit area
rect.left -= 100; // increase left hit area
rect.bottom += 100; // increase bottom hit area
rect.right += 100; // increase right hit area
parent.setTouchDelegate( new TouchDelegate( rect , button));
}
});
You want "padding" It will put the space inside the view. Margin will put the space outside, which will not increase the hit area.
<Button
android:layout_width="22dip"
android:layout_height="23dip"
android:background="@drawable/triangle"
android:padding="10dp" />
You need to use a TouchDelegate
, which is defined in the API docs as "Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds"
http://developer.android.com/reference/android/view/TouchDelegate.html