change edittext cursor color

In your EditText properties, there is an attribute android:textCursorDrawable

Now set it to @null like,

android:textCursorDrawable="@null"

So now your EditText Cursor is same as your EditText TextColor.

Reference From Set EditText cursor color


I found a way to fix this. It is not the greatest solution, but it works.

Unfortunately, I can only use static color for the cursor color.

First, I define a black cursor in drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#ff000000"/>
    <size android:width="1dp"/>
</shape>

Next I define a sample EditText in layouts.

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textCursorDrawable="@drawable/blackpipe"
         >
    </EditText>

When I want to create an EditText at runtime, I use this:

AttributeSet editText30AttributeSet = null;
int res = getResources().getIdentifier("edit30", "layout", getPackageName());//edit30 is EditText layout
XmlPullParser parser = getResources().getXml(res);
int state=0;
do {
    try {
        state = parser.next();
    } catch (Exception e1) {
        e1.printStackTrace();
    }       
    if (state == XmlPullParser.START_TAG) {
        if (parser.getName().equals("EditText")) {
            editText30AttributeSet = Xml.asAttributeSet(parser);
            break;
        }
    }
} while(state != XmlPullParser.END_DOCUMENT);
EditText view = new EditText(getContext(),editText30AttributeSet);

Now you have an EditText view that has a black cursor. Maybe somebody can improve on my solution so that the cursor can be changed at runtime.


Here is, what I think, a better solution than what @Adem posted.

Java:

try {
    // https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);
    f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#ff000000" />

    <size android:width="1dp" />

</shape>

improving on Jared Rummler's answer

Java:

try {
    // https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);
    f.set(yourEditText, R.drawable.custom_cursor);
} catch (Exception ignored) {
}

create custom_cursor.xml in res/drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#ff000000" />

    <size android:width="1dp" />
</shape>

XML:

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textCursorDrawable="@drawable/custom_cursor"
     >
</EditText>

What about the styles.xml using AppCompat-v7?

<item name="colorControlNormal">@color/accentColor</item>
<item name="colorControlActivated">@color/accentColor</item>
<item name="colorControlHighlight">@color/accentColor</item>

Works for me (this example is simplistic).