Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change EditText Cursor color through styles.xml

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);
like image 709
Siva Avatar asked Oct 22 '25 04:10

Siva


2 Answers

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

like image 56
Sibaditya Maiti Avatar answered Oct 24 '25 18:10

Sibaditya Maiti


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.

like image 43
Jaydroid Avatar answered Oct 24 '25 17:10

Jaydroid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!