Skip to content

Instantly share code, notes, and snippets.

@sushihangover
Created February 23, 2018 22:57
Show Gist options
  • Save sushihangover/01a7965aae75d8ef0589697aa8f0e750 to your computer and use it in GitHub Desktop.
Save sushihangover/01a7965aae75d8ef0589697aa8f0e750 to your computer and use it in GitHub Desktop.
Xamarin.Android translation of DroidParts ClearableEditText
// DroidParts is under Apache License 2.0
// https://github.com/droidparts/droidparts/blob/master/droidparts-misc/src/org/droidparts/widget/ClearableEditText.java
public class ClearableEditText : EditText, View.IOnTouchListener, View.IOnFocusChangeListener, ITextWatcher
{
public ClearableEditText(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer) { Initialize(); }
public ClearableEditText(Context context) : base(context) { Initialize(); }
public ClearableEditText(Context context, Android.Util.IAttributeSet attrs) : base(context, attrs) { Initialize(); }
public ClearableEditText(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { Initialize(); }
public ClearableEditText(Context context, Android.Util.IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) { Initialize(); }
public interface IListener
{
void DidClearText();
}
public enum Location
{
LEFT = 0,
RIGHT = 2,
}
IListener listener;
Location? loc = Location.RIGHT;
Drawable xD;
IOnTouchListener l;
IOnFocusChangeListener f;
public void SetListener(IListener listener)
{
this.listener = listener;
}
public void SetIconLocation(Location loc)
{
this.loc = loc;
InitIcon();
}
void Initialize()
{
base.SetOnTouchListener(this);
base.OnFocusChangeListener = this;
InitIcon();
SetClearIconVisible(false);
}
public override void SetOnTouchListener(IOnTouchListener l)
{
this.l = l;
}
public void SetOnFocusChangeListener(IOnFocusChangeListener f)
{
this.f = f;
}
void InitIcon()
{
xD = null;
if (loc != null)
{
xD = GetCompoundDrawables()[(int)loc];
}
if (xD == null)
{
xD = Resources.GetDrawable(Android.Resource.Drawable.PresenceOffline);
}
xD.SetBounds(0, 0, xD.IntrinsicWidth, xD.IntrinsicHeight);
int min = PaddingTop + xD.IntrinsicHeight + PaddingBottom;
if (SuggestedMinimumHeight < min)
{
SetMinimumHeight(min);
}
}
Drawable GetDisplayedDrawable()
{
return (loc != null) ? GetCompoundDrawables()[(int)loc] : null;
}
void SetClearIconVisible(bool visible)
{
Drawable[] cd = GetCompoundDrawables();
Drawable displayed = GetDisplayedDrawable();
var wasVisible = (displayed != null);
if (visible != wasVisible)
{
Drawable x = visible ? xD : null;
base.SetCompoundDrawables((loc == Location.LEFT) ? x : cd[0], cd[1], (loc == Location.RIGHT) ? x : cd[2], cd[3]);
}
}
public bool OnTouch(View v, MotionEvent e)
{
if (GetDisplayedDrawable() != null)
{
int x = (int)e.GetX();
int y = (int)e.GetY();
int left = (loc == Location.LEFT) ? 0 : Width - PaddingRight - xD.IntrinsicWidth;
int right = (loc == Location.LEFT) ? PaddingLeft + xD.IntrinsicWidth : Width;
bool tappedX = x >= left && x <= right && y >= 0 && y <= (Bottom - Top);
if (tappedX)
{
if (e.Action == MotionEventActions.Up)
{
Text = "";
if (listener != null)
{
listener.DidClearText();
}
}
return true;
}
}
if (l != null)
return l.OnTouch(v, e);
return false;
}
public void OnFocusChange(View v, bool hasFocus)
{
if (hasFocus)
SetClearIconVisible(!string.IsNullOrEmpty(Text));
else
SetClearIconVisible(false);
if (f != null)
f.OnFocusChange(v, hasFocus);
}
void ITextWatcher.OnTextChanged(ICharSequence s, int start, int before, int count)
{
if (IsFocused)
SetClearIconVisible(!string.IsNullOrEmpty(Text));
}
void ITextWatcher.AfterTextChanged(IEditable s)
{
}
void ITextWatcher.BeforeTextChanged(ICharSequence s, int start, int count, int after)
{
}
}
@sushihangover
Copy link
Author

AXML Example:

    <sushihangover.ClearableEditText
        android:id="@+id/clearableEditText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="StackOverflow Right Clear"
        />

Left Clear Button Example:

var clearable2 = FindViewById<ClearableEditText>(Resource.Id.clearableEditText2);
clearable2.SetIconLocation(ClearableEditText.Location.LEFT);


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment