Created
January 20, 2016 19:55
-
-
Save jpiche/a0f27c6804dd45a94e7d to your computer and use it in GitHub Desktop.
Change cursor color on an EditText
This file contains hidden or 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
| protected static void setCursorDrawableColor(EditText editText, int color) { | |
| try { | |
| Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes"); | |
| fCursorDrawableRes.setAccessible(true); | |
| int mCursorDrawableRes = fCursorDrawableRes.getInt(editText); | |
| Field fEditor = TextView.class.getDeclaredField("mEditor"); | |
| fEditor.setAccessible(true); | |
| Object editor = fEditor.get(editText); | |
| Class<?> clazz = editor.getClass(); | |
| Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable"); | |
| fCursorDrawable.setAccessible(true); | |
| Drawable[] drawables = new Drawable[2]; | |
| Resources res = editText.getContext().getResources(); | |
| drawables[0] = res.getDrawable(mCursorDrawableRes); | |
| drawables[1] = res.getDrawable(mCursorDrawableRes); | |
| drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN); | |
| drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); | |
| fCursorDrawable.set(editor, drawables); | |
| } catch (final Throwable ignored) { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment