78

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.

Abhilasha
  • 929
  • 1
  • 17
  • 37
Adem
  • 9,402
  • 9
  • 43
  • 58

8 Answers8

125

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

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
30

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.

Adam
  • 93
  • 8
Adem
  • 9,402
  • 9
  • 43
  • 58
17

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>
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
9

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>
kc ochibili
  • 3,103
  • 2
  • 25
  • 25
8

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

shkschneider
  • 17,833
  • 13
  • 59
  • 112
3

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

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Aj 27
  • 2,316
  • 21
  • 29
2

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>
Rakesh Yadav
  • 1,966
  • 2
  • 21
  • 35
1

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.

Sergey Galin
  • 426
  • 6
  • 8