10

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 :)

alessandro gaboardi
  • 889
  • 3
  • 11
  • 26
  • 1
    As you said, I think you can change this color by changing accentColor – Chol Feb 23 '16 at 16:48
  • Yes but changing accent color would change also the color of the underlying elements that are hardwired to accent color – alessandro gaboardi Feb 23 '16 at 16:52
  • Maybe you can create a custom theme for this edit text and set the accentColor as desired – Chol Feb 23 '16 at 16:53
  • i didn't think about that, i'll try in a moment and let you know, do you know of a way to change a theme's accent color programmatically? – alessandro gaboardi Feb 23 '16 at 16:56
  • Can check this : http://stackoverflow.com/questions/25354558/how-to-change-programmatically-the-primary-color-in-android-l but doesn't seems as easy as only add few lines in the styles.xml :) – Chol Feb 23 '16 at 16:58
  • i finally tried to do it and the theme works but it's too complicated to change programmatically so it's not worth it – alessandro gaboardi Feb 28 '16 at 18:40

5 Answers5

7

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>
Chol
  • 2,097
  • 2
  • 16
  • 26
4

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"
MUHAMMAD SOBAN
  • 403
  • 8
  • 20
2

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

android:textSelectHandle="@drawable/your_drawble"
hadi
  • 367
  • 5
  • 18
  • none of suggested variants worked for my TextInputEditText or AppCompatAutoCompleteTextView, and only this one. I used "invert_colors" vector from Android Studio icons – djdance Apr 26 '21 at 15:33
2

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) {
    }
}
Ivanov Maksim
  • 3,460
  • 1
  • 11
  • 10
2

You can add colorControlActivated to your AppTheme in style :

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