Skip to content

Instantly share code, notes, and snippets.

@Sa1Gur
Created December 2, 2019 14:51
Show Gist options
  • Save Sa1Gur/d0e320ab1ad1fea5c08f7322601c2b05 to your computer and use it in GitHub Desktop.
Save Sa1Gur/d0e320ab1ad1fea5c08f7322601c2b05 to your computer and use it in GitHub Desktop.
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control == null || e.NewElement == null) return;
if (e.NewElement is MyMaterialEntry customEntry)
{
EditText.SetHighlightColor(color: customEntry.MyHighlightColor.ToAndroid());
try
{
JNIEnv.SetField(EditText.Handle, JNIEnv.GetFieldID(JNIEnv.FindClass(typeof(TextView)), "mCursorDrawableRes", "I"), 0);
using (var textViewTemplate = new TextView(EditText.Context))
{
var field = textViewTemplate.Class.GetDeclaredField("mEditor");
field.Accessible = true;
var editor = field.Get(EditText);
string[]
fieldsNames = { "mTextSelectHandleLeftRes", "mTextSelectHandleRightRes", "mTextSelectHandleRes" },
drawablesNames = { "mSelectHandleLeft", "mSelectHandleRight", "mSelectHandleCenter" };
for (int index = 0; index < fieldsNames.Length && index < drawablesNames.Length; index++)
{
field = textViewTemplate.Class.GetDeclaredField(fieldsNames[index]);
field.Accessible = true;
Drawable handleDrawable = ContextCompat.GetDrawable(Context, field.GetInt(EditText));
handleDrawable.SetColorFilter(new PorterDuffColorFilter(customEntry.MyHighlightColor.ToAndroid(), PorterDuff.Mode.SrcIn));
field = editor.Class.GetDeclaredField(drawablesNames[index]);
field.Accessible = true;
field.Set(editor, handleDrawable);
}
}
}
catch (NoSuchFieldError)
{
}
catch (NoSuchFieldException)
{
}
catch (ReflectiveOperationException)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment