Created
April 23, 2019 13:50
-
-
Save felipeslongo/39e222dd5c1552dad0786421a641b82f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Android.Graphics.Drawables; | |
using Android.Views; | |
using Android.Widget; | |
namespace App.Droid.Utils | |
{ | |
public static class TextViewDrawableTouchEventHandler | |
{ | |
const int DrawableLeft = 0; | |
const int DrawableTop = 1; | |
const int DrawableRight = 2; | |
const int DrawableBottom = 3; | |
public static void SetupRightDrawableClick(EditText view, Action action) => | |
view.Touch += (sender, args) => TriggerActionOnRightDrawableClick(sender, args, action); | |
private static void TriggerActionOnRightDrawableClick(object sender, View.TouchEventArgs e, Action action) | |
{ | |
var view = (EditText) sender; | |
e.Handled = false; | |
if (!IsClickEvent(e)) | |
return; | |
var drawable = view.GetCompoundDrawables()[DrawableRight]; | |
if (drawable == null) | |
return; | |
if (!IsRightDrawableClickZone(e, view, drawable)) | |
return; | |
action(); | |
e.Handled = true; | |
} | |
private static bool IsRightDrawableClickZone(View.TouchEventArgs e, EditText view, Drawable drawable) | |
{ | |
var userXAxisTouch = e.Event.RawX; | |
var viewXAxisPointThatSeparateTheViewFromTheRightDrawable = view.Right - drawable.Bounds.Width(); | |
return userXAxisTouch >= viewXAxisPointThatSeparateTheViewFromTheRightDrawable; | |
} | |
private static bool IsClickEvent(View.TouchEventArgs e) => | |
e.Event.Action == MotionEventActions.Up; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment