Is there an example of how to use a TouchDelegate in Android to increase the size of a view's click target?
I asked a friend at Google and they were able to help me figure out how to use TouchDelegate. Here's what we came up with:
final View parent = (View) delegate.getParent();
parent.post( new Runnable() {
// Post in the parent's message queue to make sure the parent
// lays out its children before we call getHitRect()
public void run() {
final Rect r = new Rect();
delegate.getHitRect(r);
r.top -= 4;
r.bottom += 4;
parent.setTouchDelegate( new TouchDelegate( r , delegate));
}
});
How I can increase the touch area for controls (ToggleButton)?
Easy solution : If you have image for the button, then create transparent area around it (i.e. for touch area).
The grey area can be transparent to increase the touch area.
Or use TouchDelegate
Struggling with multiple use of Androids TouchDelegate
I found that when I add another LinearLayout
around each row, both TouchDelegates
work.
Now my example layout file looks like this:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/layout_row_1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Switch android:id="@+id/switch_row_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/layout_row_2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Switch android:id="@+id/switch_row_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Maybe there is another solution. This one seems somehow tedious.
Strange behavior when enlarging rectangle for TouchDelegate
The reason of this such behavior, is due to the fact currency_exchange_linear_layout
is NOT direct child of touch_delegate_linear_layout
Hence, when we perform
currencyExchangeLinearLayout.getHitRect(r);
The obtained rectangle's coordinate is not relative to touch_delegate_linear_layout
In such case, we may use footer_value_text_view
's hit rectangle, which its rectangle's Y-coordinate is relative to touch_delegate_linear_layout
final View parentView = v.findViewById(R.id.touch_delegate_linear_layout);
final View directChildView = v.findViewById(R.id.footer_value_text_view);
ViewTreeObserver vto = parentView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
final Rect r = new Rect();
final Rect directChild = new Rect();
currencyExchangeLinearLayout.getHitRect(r);
directChildView.getHitRect(directChild);
int bestHeight = Utils.dpToPixel(48);
directChild.top = directChild.bottom - Math.max((bestHeight - r.height()), 0);
parentView.setTouchDelegate(new TouchDelegate(directChild, currencyExchangeLinearLayout));
ViewTreeObserver obs = parentView.getViewTreeObserver();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
Increase the clickable area of the button
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
Related Topics
Android Browser Ignores Responsive Web Design
How to Display List of Resource Drawables
Gridlayoutmanager - How to Auto Fit Columns
How to Know the Calling Activity in Android
Android: Listview.Getscrolly() - Does It Work
How to Get Activity in Compose
Firestorepagingadapter Not Receiving Realtime Updates
Mysterious Stacktrace in Android Developer Console (Bitmap Size Exceeds 32Bits)
Android in Navigation Drawer When I Click Anywhere Drawer Closes
Value <Br of Type Java.Lang.String Cannot Be Converted to JSONobject on Android
How to Edit the Text Files in Assets Folder in Android
How to Have a Masked Numeric Input Field
Borders on Certain Zoom Levels on Webpage