alessandro gaboardi
alessandro gaboardi

Reputation: 959

Changing Android's EditText cursor pointer color programmatically

So, i've searched a lot but i couldn't find anything, if this ends up being a duplicate i'm sorry, but i want to cange the color of the EditText's cursor pointer that is used to move the cursor

enter image description here

I already managed to change the cursor's color itself but it doesn't affect the pointer which seems to only respond to accentColor.

I'm pretty sure it's possible because i recall seeing an app that did it, it had the pointer and all the elements in a dialog change color based on a color that you choose without changing the accent color of the view below the dialog.

Please help :)

Upvotes: 10

Views: 9843

Answers (5)

Mohamed Matar
Mohamed Matar

Reputation: 21

You can add colorControlActivated to your AppTheme in style :

<item name="colorControlActivated">"Your_color"</item>

Upvotes: 2

Ivanov Maksim
Ivanov Maksim

Reputation: 3629

You can use reflection to set selectHandle.

EditText has Editor class that has the mSelectHandleCenter field (if you need selection use mSelectHandleLeft and mSelectHandleRight)

private fun setCursorPointerColor(view: EditText, @ColorInt color: Int) {
    try {
        //get the pointer resource id
        var field = TextView::class.java.getDeclaredField("mTextSelectHandleRes")
        field.isAccessible = true
        val drawableResId = field.getInt(view)

        //get the editor
        field = TextView::class.java.getDeclaredField("mEditor")
        field.isAccessible = true
        val editor = field.get(view)

        //tint drawable
        val drawable = ContextCompat.getDrawable(view.context, drawableResId)!!
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN)

        //set the drawable
        field = editor.javaClass.getDeclaredField("mSelectHandleCenter")
        field.isAccessible = true
        field.set(editor, drawable)

    } catch (ex: Exception) {
    }
}

Upvotes: 2

hadi
hadi

Reputation: 497

you can use textSelectHandle to replace it by your drawable like this:

android:textSelectHandle="@drawable/your_drawble"

Upvotes: 2

MUHAMMAD SOBAN
MUHAMMAD SOBAN

Reputation: 423

You can do this by creating new style in style.xml like this

<style name="CustomEditTextTheme" parent="TextAppearance.AppCompat">
    <item name="colorAccent">@color/primary_dark_material_dark</item>       
</style>

Then in EditText tag use this

style="@style/CustomEditTextTheme"

Upvotes: 4

Chol
Chol

Reputation: 2117

You can create your own style/theme for only this EditText and change the ColorAccent :

<style name="EditTextColorCustom" parent="@style/AppBaseTheme">
        <!-- Customize your theme here. -->
        <item name="colorAccent">@color/colorAccent</item>
    </style>

Upvotes: 7

Related Questions