set Cursor Drawable Color for EditText - Android Graphics

Android examples for Graphics:Drawable

Description

set Cursor Drawable Color for EditText

Demo Code


//package com.java2s;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.reflect.Field;

public class Main {
    public static void setCursorDrawableColor(EditText editText, int color) {
        try {// ww  w .j  a  va2 s  . co m
            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];
            drawables[0] = editText.getContext().getResources()
                    .getDrawable(mCursorDrawableRes);
            drawables[1] = editText.getContext().getResources()
                    .getDrawable(mCursorDrawableRes);
            drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
            drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
            fCursorDrawable.set(editor, drawables);
        } catch (Throwable ignored) {
        }
    }
}

Related Tutorials