Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change edittext cursor color

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.

like image 750
Adem Avatar asked Jul 25 '12 12:07

Adem


People also ask

How do you change the color of your cursor on Android?

Setting the android:textCursorDrawable attribute to @null should result in the use of android:textColor as the cursor color. oh man that is SO much more efficient than conjuring up a drawable for the cursor to be black!!


2 Answers

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

like image 155
user370305 Avatar answered Oct 03 '22 15:10

user370305


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.

like image 28
Adem Avatar answered Oct 03 '22 16:10

Adem