4

Right now, I can change the cursor drawable through reflection and using property android:textCursorDrawable. I looked at the TextView code, and its actually reading the drawables from Resources.Theme. I'm wondering if its possible and how to change it by creating a custom theme in styles.xml?

 final Resources.Theme theme = context.getTheme();
 /*
  * Look the appearance up without checking first if it exists because
  * almost every TextView has one and it greatly simplifies the logic
  * to be able to parse the appearance first and then let specific tags
  * for this View override it.
  */

 TypedArray a = theme.obtainStyledAttributes(attrs,
            com.android.internal.R.styleable.TextViewAppearance, defStyleAttr, defStyleRes);
musica
  • 1,373
  • 3
  • 15
  • 34
Siva
  • 355
  • 1
  • 6
  • 19
  • Possible duplicate of [Set EditText cursor color](http://stackoverflow.com/questions/7238450/set-edittext-cursor-color) – R. Zagórski Sep 26 '16 at 09:57

5 Answers5

4

In your layout do the following :

<EditText  
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textCursorDrawable="@drawable/cursor_color"/>

Then create drawalble cursor_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:width="2dp" />
    <solid android:color="#FFFFFF"/> // Set your required color code for your cursor color
</shape>

Hope this will help you

3

Edittext has an attribute called android:textCursorDrawable , set this attribute to @null now you can set any desired color to android:textColor, this will result in your cursor color change.

Jaydroid
  • 334
  • 3
  • 13
1

add this in your drawable folder.:

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

and define this tag in EditText tag:

 android:textCursorDrawable="@drawable/black_cursor"
Shivanshu Verma
  • 609
  • 5
  • 19
0

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

Now set it to @null like this:

android:textCursorDrawable="@null"

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

Laurel
  • 5,965
  • 14
  • 31
  • 57
0

Change the color anytime you want in your project at res->values->colors.xml named "colorAccent". This applies to all Edit Text as default color, and supports all API Versions.

Sauer Voussoir
  • 111
  • 2
  • 12