Increase the click area

I want to increase the button click area. But the image on the button should remain the same size. I also set the image as a background not as a source. How can i do this?

<Button android:id="@+id/backbutton" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:background="@drawable/arrow" android:paddingLeft="10dp" android:paddingRight="10dp" android:textColor="@color/title_gray" android:textSize="14sp" android:visibility="visible" /> 
+6
source share
4 answers

Just create the parent layout of the button (larger or clickable) and execute a click event similar to -

 <LinearLayout android:id="@+id/backbuttonlayout" android:layout_width="50dp" android:layout_height="50dp"> <Button android:id="@+id/backbutton" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:background="@drawable/arrow" android:paddingLeft="10dp" android:paddingRight="10dp" android:textColor="@color/title_gray" android:textSize="14sp" android:visibility="visible" /> </LinearLayout> 

Now, in your activity, do this:

 LinearLayout backbuttonlayout = (LinearLayout)findViewById(R.id.backbuttonlayout); 

and do setOnClickListener () on backbuttonlayout

+4
source

Use TouchDelegate

A helper class for handling situations where you want a view to have a larger touch area than its actual viewing boundaries. The view whose touch area has been changed is called the delegate view. This class should be used by the ancestor of the delegate. To use TouchDelegate, first create an instance that defines the boundaries that must be mapped to the delegate and the delegate view itself.

Example

 public class LaunchActivity extends Activity { private Button MyButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_launch); MyButton = (Button)findViewById(R.id.Button1); //Your button ID View parent = findViewById(R.id.layout); //Your Layout ID parent.post(new Runnable() { @Override public void run() { Rect delegateArea = new Rect(); Button delegate = MyButton; delegate.getHitRect(delegateArea); delegateArea.top -= 600; //Choose yourself delegateArea.bottom += 600; delegateArea.left -= 600; delegateArea.right += 600; TouchDelegate expandedArea = new TouchDelegate(delegateArea, delegate); // give the delegate to an ancestor of the view we're // delegating the // area to if (View.class.isInstance(delegate.getParent())) { ((View) delegate.getParent()) .setTouchDelegate(expandedArea); } } }); } } 

I think this will help you.

+4
source

You can use indentation. It places the space inside the view (the margin will go out).

For example, the following code will provide a zone with a click of 20dp, but the background will be 10dp.

 <Button android:layout_width="20dp" android:layout_height="20dp" android:background="@drawable/your_background" android:padding="10dp" /> 
+1
source
  • Delete the field, use the registration around the button.
  • Surround the button using the word LinearLayout, which has a registration around the button.
  • Add the same onclick to LinearLayout as the button.
0
source

Source: https://habr.com/ru/post/988381/


All Articles