How can I change EditText's
cursor's color programmatically ?
In android 4.0 and above, cursor color is white. and if EditText
s background is also white, it becomes invisible.
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!!
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With