How to change color / appearance of EditText select handle / anchor?

前端 未结 6 1324
不思量自难忘°
不思量自难忘° 2020-11-27 17:46

So I changed the style of the Holo Theme with the Holo Colors Generator and Action Bar Style Generator to my own color. But when I select text inside an edit text, the \"mar

相关标签:
6条回答
  • 2020-11-27 17:56

    The worst part here was to find the "name" for this item and how it is called inside the theme. So I looked through every drawable in the android SDK folder and finally found the drawables named "text_select_handle_middle", "text_select_handle_left" and "text_select_handle_right".

    So the solution is simple: Add these drawables with customized design/color to your drawable folder and add them to your theme style definition like:

    <style name="MyCustomTheme" parent="@style/MyNotSoCustomTheme">
            <item name="android:textSelectHandle">@drawable/text_select_handle_middle</item>
            <item name="android:textSelectHandleLeft">@drawable/text_select_handle_left</item>
            <item name="android:textSelectHandleRight">@drawable/text_select_handle_right</item>
    </style>
    
    0 讨论(0)
  • 2020-11-27 17:57

    I recognize this is really late, but if all you want to do is change the color of the handle, you just need to add the following to your styles.xml file.

    <style name="ColoredHandleTheme">
        <item name="colorControlActivated">@color/colorYouWant</item>
    </style>
    

    And then just set the theme on whatever activity is holding the EditText which you want to affect.

    Or if you want to set it app-wide, you can do the following:

    <style name="ColoredHandleThemeForWholeApp">
        <item name="colorAccent">@color/colorYouWant</item>
    </style>
    

    And set that theme for the whole app.

    Problem solved!

    0 讨论(0)
  • 2020-11-27 18:06

    How to do it from code:

    try {
        final Field fEditor = TextView.class.getDeclaredField("mEditor");
        fEditor.setAccessible(true);
        final Object editor = fEditor.get(editText);
    
        final Field fSelectHandleLeft = editor.getClass().getDeclaredField("mSelectHandleLeft");
        final Field fSelectHandleRight =
            editor.getClass().getDeclaredField("mSelectHandleRight");
        final Field fSelectHandleCenter =
            editor.getClass().getDeclaredField("mSelectHandleCenter");
    
        fSelectHandleLeft.setAccessible(true);
        fSelectHandleRight.setAccessible(true);
        fSelectHandleCenter.setAccessible(true);
    
        final Resources res = context.getResources();
    
        fSelectHandleLeft.set(editor, res.getDrawable(R.drawable.text_select_handle_left));
        fSelectHandleRight.set(editor, res.getDrawable(R.drawable.text_select_handle_right));
        fSelectHandleCenter.set(editor, res.getDrawable(R.drawable.text_select_handle_middle));
    } catch (final Exception ignored) {
    }
    
    0 讨论(0)
  • 2020-11-27 18:06

    In order to change the color of select handles, you have to override the activated color in your app theme:

    <style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorControlActivated">@color/customActivatedColor</item>
    </style>
    
    0 讨论(0)
  • 2020-11-27 18:08

    For those who try to handle this from code, you can use answer provided by Jared Rummler for API version less or equal to 28, but from Android Q(API 29) they've added a @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) annotation and it doesn't seem to work on Q+. Instead a new methods where added to EditText:

    setTextSelectHandle(R.drawable.test);
    setTextSelectHandleLeft(R.drawable.test);
    setTextSelectHandleRight(R.drawable.test);
    

    So, to make it work you could do something like this:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    setTextSelectHandle(R.drawable.test);
                    setTextSelectHandleLeft(R.drawable.test);
                    setTextSelectHandleRight(R.drawable.test);
                } else {
                    try {
                        final Field fEditor = TextView.class.getDeclaredField("mEditor");
                        fEditor.setAccessible(true);
                        final Object editor = fEditor.get(this);
    
                        final Field fSelectHandleLeft = editor.getClass().getDeclaredField("mSelectHandleLeft");
                        final Field fSelectHandleRight =
                                editor.getClass().getDeclaredField("mSelectHandleRight");
                        final Field fSelectHandleCenter =
                                editor.getClass().getDeclaredField("mSelectHandleCenter");
    
                        fSelectHandleLeft.setAccessible(true);
                        fSelectHandleRight.setAccessible(true);
                        fSelectHandleCenter.setAccessible(true);
    
                        final Resources res = getResources();
    
                        fSelectHandleLeft.set(editor, res.getDrawable(R.drawable.test, null));
                        fSelectHandleRight.set(editor, res.getDrawable(R.drawable.test, null));
                        fSelectHandleCenter.set(editor, res.getDrawable(R.drawable.test, null));
                    } catch (final Exception ignored) {
                    }
                }
    
    0 讨论(0)
  • 2020-11-27 18:18

    You can see these attributes in http://androiddrawables.com/Other.html.

    Change your values/styles.xml, for instance:

    <style name="AppTheme.Cursor" parent="AppTheme">
        <item name="colorAccent">@color/cursor</item>
    </style>
    

    where @color/cursor is added in values/color.xml. After that apply the style to the activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme_Cursor);
        ...
    

    Visit How to change EditText pointer color (not cursor) for other solutions.

    0 讨论(0)
提交回复
热议问题