Reputation:
I want to increase the clickable area of the button.But the image in the button should remain of same size.Also i have set image as a background not as source .How can i do that?
<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" />
Upvotes: 9
Views: 16387
Reputation: 4490
Use TouchDelegate
Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds. The view whose touch area is changed is called the delegate view. This class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an instance that specifies the bounds that should 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 out
Upvotes: 9
Reputation: 525
You can use padding. It will put the space inside the view (margin will put it outside).
For example the following code will provide a clickable area of 20dp but the background will be of 10dp.
<Button
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/your_background"
android:padding="10dp" />
Upvotes: 4
Reputation: 3352
Upvotes: 0
Reputation: 4001
Just make the parent layout of the button (of larger size or clickable size), and perform click event of that like -
<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, inside your activity, do like -
LinearLayout backbuttonlayout = (LinearLayout)findViewById(R.id.backbuttonlayout);
and perform setOnClickListener() on backbuttonlayout
Upvotes: 6