Use android. view. TouchDelegate to expand the touch click area of the View.

Source: Internet
Author: User

The standard valid and touchable UI elements specified by Android4.0 are 48dp, which is converted to a physical size of about 9mm. The recommended target size is 7 ~ 10mm, which is consistent with the iPhone, is an area where the user's fingers are accurate and comfortable to touch. As shown in, your UI elements may be smaller than 48dp, the icons only have 32dp, And the buttons only have 40dp, but their actual operational focus has reached the 48dp size.


In order to make the small UI area have a good touch interaction, according to the View features, there are currently two situations:

1. For example, set the padding value of ImageView. The area that can be touched will be extended;

2. For example, if a Button is set to its padding value, the touch area remains unchanged and the display area of the Button is compressed inward;


In Case 1, you can directly set its padding value to achieve the goal. android: padding = "10dp"

In Case 2, you can use TouchDelegate to dynamically modify the touch area to expand the click range.

/*** Expand the touch and click response range of the View, maximum Value: ** @ param View * @ param top * @ param bottom * @ param left * @ param right */public static void expandViewTouchDelegate (final view View view, final int top, final int bottom, final int left, final int right) {(View) view. getParent ()). post (new Runnable () {@ Overridepublic void run () {Rect bounds = new Rect (); view. setEnabled (true); view. getHitRect (bounds); bounds. top-= top; bounds. bottom + = bottom; bounds. left-= left; bounds. right + = right; TouchDelegate touchDelegate = new TouchDelegate (bounds, view); if (View. class. isInstance (view. getParent () {(View) view. getParent ()). setTouchDelegate (touchDelegate );}}});}

Note the following two points when using this method:

1. If the custom touch range of the View exceeds the size of the Parent, the part that exceeds the value is invalid.
2. Only one View TouchDelegate can be set for one Parent. If multiple views are set, only the last settings take effect.


To restore the touch range of the View:

/*** Restore the touch and click response range of the View. The minimum value is not smaller than the View's own range. ** @ param view */public static void restoreViewTouchDelegate (final View view) {(View) view. getParent ()). post (new Runnable () {@ Overridepublic void run () {Rect bounds = new Rect (); bounds. setEmpty (); TouchDelegate touchDelegate = new TouchDelegate (bounds, view); if (View. class. isInstance (view. getParent () {(View) view. getParent ()). setTouchDelegate (touchDelegate );}}});}

Using TouchDelegate to expand the touch response range of a View is a flexible method, which can be used in combination with setting padding.


For details, refer to the following link for more information:

1. Android TouchDelegate to increase the touch range of View http://blog.csdn.net/sgwhp/article/details/10963383

2. ListView Tips & Tricks #5: Enlarged Touchable Areas http://cyrilmottier.com/2012/02/16/listview-tips-tricks-5-enlarged-touchable-areas/

3. Extend touchable areas # Android https://plus.google.com/u/0/+JulienDodokal/posts/8zoV3RQvReS



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.