Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase hit area of Android button without scaling background?

Tags:

android

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.

_____________ |            | |    |\      | |    | \     | |    |__\    | |____________| 
like image 306
JoJo Avatar asked Nov 17 '11 23:11

JoJo


People also ask

How do I make the buttons bigger on my Android?

Tap the Gear icon that appears at the top of the Android keyboard. Open Preferences. Tap the Keyboard Height option. You'll see seven different options ranging from "Extra-short" to "Extra-tall." The default is "Normal." Tap the option you prefer.

How to change Button shape Android?

We can set custom shapes on our button using the xml tag <shape> . These xml files are created in the drawable folder too. shape can be used inside selectors . The shape can be set to rectangle (default), oval , ring , line .


2 Answers

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));      }  });  
like image 197
Morris Lin Avatar answered Oct 01 '22 10:10

Morris Lin


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" /> 
like image 28
kwahn Avatar answered Oct 01 '22 09:10

kwahn