Skip to content

Instantly share code, notes, and snippets.

@felipeslongo
Created April 23, 2019 13:50
Show Gist options
  • Save felipeslongo/39e222dd5c1552dad0786421a641b82f to your computer and use it in GitHub Desktop.
Save felipeslongo/39e222dd5c1552dad0786421a641b82f to your computer and use it in GitHub Desktop.
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