Reputation: 611
I've been working on a practice app where the user can set the color of text and background. But I'm failing to find a way around it
android:textCursorDrawable="@null"
I've tried this and while it sets the color to the same as the text color it's not what I'm looking to do as I have a specific size set for my cursor.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="10dip" />
<size android:height="14dip" />
<solid android:color="#fff" />
</shape>
Ive also tried android:color="@null" but that returns nothing ... obviously really (That's a note to my own stupidity).
Finally, I've seen this.
Field f = null;
f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(editText, R.drawable.cursor);
Could i set a color via this way? or is it just a java version of the xml call I already have? Anyhelp would be appreciated.
Basically, to sum up, Id like to set the color of the cursor same as the edit text color but have my height and width of the cursor at 10x14?
Thank you
Upvotes: 0
Views: 314
Reputation: 949
Please check this for cursor color change,
public static void setCursorColor(EditText view, @ColorInt int color) {
try {
// Get the cursor resource id
Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
field.setAccessible(true);
int drawableResId = field.getInt(view);
// Get the editor
field = TextView.class.getDeclaredField("mEditor");
field.setAccessible(true);
Object editor = field.get(view);
// Get the drawable and set a color filter
Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableResId);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
Drawable[] drawables = {drawable, drawable};
// Set the drawables
field = editor.getClass().getDeclaredField("mCursorDrawable");
field.setAccessible(true);
field.set(editor, drawables);
} catch (Exception ignored) {
}
}
Upvotes: 2