14

I want to change the android's edittext cursor color on all devices. How do I do that?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Kishore
  • 161
  • 1
  • 3
  • 6

5 Answers5

19

i had to use a drawable like this:

mycursor.xml:

      <?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="@android:color/holo_blue_light"/> 
<!--make sure its a solid tag not stroke or it wont work -->
    </shape>

in my edit text i set the cursor drawable attributes like this:

                            <EditText
                            android:id="@+id/et_details"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:cursorVisible="true"
                           android:textCursorDrawable="@drawable/mycursor"  
                            />
j2emanue
  • 60,549
  • 65
  • 286
  • 456
8

The way to get it across all platforms is as such.

1 - Open your layout.xml in your layout folder. Find the edit text and set

android:cursorVisible="true"

this will set the cursor for devices lower that os version 11

2 - Create your cursor_drawable.xml in the 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" />
    <stroke android:color="@color/black"/>
</shape>

3 - Create a folder layout-v11

4 - Copy your layout.xml into the layout-v11

5 - Find your edit text and set android:textCursorDrawable="@drawable/cursor_drawable"

This will make a cursor be shown on all devices and OS.

Aiden Fry
  • 1,672
  • 3
  • 21
  • 45
  • 1
    Hi aiden, im also having same issue.. im developing app with 2.2 api build. when i do as you said im getting error in xml file at android:textCursorDrawable has no reference ... Pls let me know how to solve it.... – praveenb Apr 09 '13 at 09:57
  • That also requires the Holo Theme, and hence Android 4.0. Did you read that one? – Sky Kelsey May 23 '13 at 01:11
  • 1
    I couldn't make it work nicely if gravity is set to right and hint is set to some value. Initially cursor is in front of hint and can't be moved/aligned to the right until you start typing then hint dissapears and cursor gets well aligned to the right. – Dragan Marjanović Jun 25 '13 at 19:48
  • 2
    It seems you need to show the cursor – herbertD May 09 '14 at 08:54
  • Replace stroke by solid – e-info128 Jul 27 '15 at 02:52
7

Assign android:textCursorDrawable attribute to @null and set android:textColor as the cursor color.

AkashG
  • 7,868
  • 3
  • 28
  • 43
1

Create drawalble xml: edtcolor_cursor

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

<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:cursorVisible="true"
    android:textCursorDrawable="@drawable/edtcolor_cursor"
    />
Makvin
  • 3,475
  • 27
  • 26
-1

Refer to this link. You can try this to

android:textCursorDrawable
Community
  • 1
  • 1
Tasneem
  • 146
  • 1
  • 1
  • 11