Change the Color of Highlighted text in Android

前端 未结 5 1633
栀梦 2021-02-20 19:13

I\'m not sure that this is possible, perhaps someone can set me straight. I have an EditText View in an android application that has white text on a blue background. When the te

  • 2021-02-20 19:18
    Object mSelectionForegroundColorSpan;
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);
        if(mSelectionForegroundColorSpan == null){
            mSelectionForegroundColorSpan = new ForegroundColorSpan(Color.GREEN);
        if(selStart > selEnd){
            int swap = selStart;
            selStart = selEnd;
            selEnd = swap;
        getText().setSpan(mSelectionForegroundColorSpan, selStart, selEnd, Spanned.SPAN_INTERMEDIATE);

    Override the onSelectionChanged method of TextView, get the text and set a ForegroundColorSpan

    0 讨论(0)
  • 2021-02-20 19:26

    Try with textColorHighlight - "Color of highlighted text."

    0 讨论(0)
  • 2021-02-20 19:28

    Try:<item name="android:textColorHighlight">your_color</item>

    in the style that you as your theme. For e.g.

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="colorPrimary">your_color1</item>
        <item name="colorPrimaryDark">your_color2</item>
        <item name="colorAccent">your_color3</item>
        <item name="android:textColor">your_color4/item>
        <item name="android:textColorHighlight">your_color</item>

    then use this style as the theme for your activity in the manifest file. For e.g.-

            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
    0 讨论(0)
  • 2021-02-20 19:30

    As referred to by this SO post, you should be using a selector to change text colors like so:

    <?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="">
        <!-- Pressed State -->
        <item android:state_pressed="true"
            android:color="#FFFFFF" />
        <!-- Focused State -->
        <item android:state_focused="true"
            android:color="#FFFFFF" />
        <!-- Default State -->
        <item android:color="#000000" />

    And then set your textColor property to @drawable/selector_name

    0 讨论(0)
  • 2021-02-20 19:41

    Did you try @android:attr/textColorPrimaryInverse ?

    0 讨论(0)