change edittext cursor color

AndroidUser Interface

Android Problem Overview


How can I change EditText's cursor's color programmatically ?

In android 4.0 and above, cursor color is white. and if EditTexts background is also white, it becomes invisible.

Android Solutions


Solution 1 - Android

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 https://stackoverflow.com/questions/7238450/set-edittext-cursor-color

Solution 2 - Android

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.

Solution 3 - Android

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>

Solution 4 - Android

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>

Solution 5 - Android

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).

Solution 6 - Android

You may Set the android:textCursorDrawable attribute to @null that will result in the use of android:textColor as the cursor color.

Attribute textCursorDrawable is available in API level 12 and higher...

Thanks...

Solution 7 - Android

Set Via AppTheme

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. -->
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:spinnerStyle">@style/spinner_style</item>
    <!--Add This-->
    <item name="editTextStyle">@style/EdittextStyle</item>
    <item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>
</style>

<!--New EditText Style-->
<style name="EdittextStyle" parent="Widget.AppCompat.EditText">
    <item name="android:background">?attr/editTextBackground</item>
    <item name="android:textColor">?attr/editTextColor</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
    <!--<item name="android:textCursorDrawable">@drawable/abc_text_cursor_material</item>-->
    <item name="colorControlNormal">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorAccent</item>

</style>

Solution 8 - Android

So, here's the final version - doesn't need the XML and works as @null but programmatically:

try {
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);
    f.set((TextView)yourEditText, 0);
} catch (Exception ignored) {
}

The only problem is that it may stop working one day with some new version of Android.

PS I tested it on 4.1.2 to 5.1.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdemView Question on Stackoverflow
Solution 1 - Androiduser370305View Answer on Stackoverflow
Solution 2 - AndroidAdemView Answer on Stackoverflow
Solution 3 - AndroidJared RummlerView Answer on Stackoverflow
Solution 4 - Androidkc ochibiliView Answer on Stackoverflow
Solution 5 - AndroidshkschneiderView Answer on Stackoverflow
Solution 6 - AndroidAj 27View Answer on Stackoverflow
Solution 7 - AndroidRakesh YadavView Answer on Stackoverflow
Solution 8 - AndroidSergey GalinView Answer on Stackoverflow