Set Edittext Cursor Color

Set EditText cursor color

Setting the android:textCursorDrawable attribute to @null should result in the use of android:textColor as the cursor color.

Attribute "textCursorDrawable" is available in API level 12 and higher

Android TextInputLayout and TextInputEditText cursor color problem

The color is based on colorPrimary.

If you want to override the colorPrimary you can use:

  <com.google.android.material.textfield.TextInputLayout                
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:theme="@style/ThemeOverlay.AppTheme.TextInputEditText.Outlined"
....>

with:

<style name="ThemeOverlay.AppTheme.TextInputEditText.Outlined" parent="">
<item name="colorPrimary">@color/...</item>
</style>

Sample Image

If you want to override only the cursor you can use:

<style name="ThemeOverlay.AppTheme.TextInputEditText.Outlined" parent="">
<item name="colorControlActivated">@color/...</item>
</style>

Sample Image

Sample Image

change edittext cursor color

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

How to change EditText pointer color (not cursor)

in your styles.xml put like this:

<item name="colorAccent">@color/blue</item>

How to change the cursor color in textinputedittext inside textinputlayout

If you want to change only the cursor color, you can use:

<com.google.android.material.textfield.TextInputLayout                
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:theme="@style/ThemeOverlay.AppTheme.TextInputEditText.Outlined"
....>

with:

<style name="ThemeOverlay.AppTheme.TextInputEditText.Outlined" parent="">
<item name="colorControlActivated">@color/...</item>
</style>

Sample Image

If you want to change the cursor color but also the box, the label..

<style name="ThemeOverlay.AppTheme.TextInputEditText.Outlined" parent="">
<item name="colorPrimary">@color/...</item>
</style>

Sample Image

How to change edit text cursor color and width in android

Build your new drawable for cursor for example name it my_custom_cursor_drawable.xml in the drawable folder

my_custom_cursor_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<size android:width="0.5dp" />
<solid android:color="#FF0910DD"/>!--Could be any color you want
</shape>

The EditBox where you want this custom cursor set the property as

 android:textCursorDrawable="@drawable/cursor_drawable"

Thats it !! improvise if need be. Any drawable , any shape etc...

How to Change programmatically Edittext Cursor Color in android?

Using some reflection did the trick for me

Java:

// https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(yourEditText, R.drawable.cursor);

XML:

<?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>

Here is a method that you can use that doesn't need an XML:

public static void setCursorColor(EditText view, @ColorInt int color) {
try {
// Get the cursor resource id
Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
field.setAccessible(true);
int drawableResId = field.getInt(view);

// Get the editor
field = TextView.class.getDeclaredField("mEditor");
field.setAccessible(true);
Object editor = field.get(view);

// Get the drawable and set a color filter
Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableResId);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
Drawable[] drawables = {drawable, drawable};

// Set the drawables
field = editor.getClass().getDeclaredField("mCursorDrawable");
field.setAccessible(true);
field.set(editor, drawables);
} catch (Exception ignored) {
}
}


Related Topics



Leave a reply



Submit