11

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 text is selected (via a long press and the edit dialog), I'd like have the highlight be white and change the text color to be black. Annoyingly though there does not seem to be a way to set the color of the text on highlight. You can set the highlight color using textColorHighlight, but not the text color, so setting a white highlight with white text results in a big white block.

It seems like it should be something trivial you can do declaratively in the xml, but though I've tried many combinations of styles and colors, I've not been able to change the color.

Checking other standard applications it seems that the text color never seems to change so I'm thinking this is something that's not easily done. I'd rather not have to subclass the EditText if at all possible just to something so simple. Am I missing something? Can this be done in the view xml?

sgargan
  • 12,208
  • 9
  • 32
  • 38

5 Answers5

2

Did you try @android:attr/textColorPrimaryInverse ?

fedj
  • 3,452
  • 1
  • 22
  • 21
  • I've tried all of and a style style="@style/myDisplayAppearance" where all the above were specified as items. Sadly, no luck with any of them. – sgargan Aug 10 '10 at 21:14
  • As I said, did you try android:textColor="?android:attr/textColorPrimaryInverse" ? – fedj Aug 10 '10 at 21:16
  • Yes I tried the above, it just changes the default text color to textColorPrimaryInverse, which in my case is black. I'm really trying to make it white be default and black only on highlight. When, if ever, are the inverse colors applied? thanks for your help btw. – sgargan Aug 10 '10 at 21:47
  • You should take a look to : http://developer.android.com/reference/android/R.attr.html in the text* attributes – fedj Aug 10 '10 at 22:34
2

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>
</style>

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

<activity
    android:name=".youractivity"
    android:label=""
    android:theme="@style/AppTheme.NoActionBar"       
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Shubham Arora
  • 807
  • 9
  • 10
0

Try with textColorHighlight - "Color of highlighted text." http://developer.android.com/reference/android/R.attr.html#textColorHighlight

bushek
  • 73
  • 4
0
Object mSelectionForegroundColorSpan;
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
    super.onSelectionChanged(selStart, selEnd);
    if(mSelectionForegroundColorSpan == null){
        mSelectionForegroundColorSpan = new ForegroundColorSpan(Color.GREEN);
    }else{
        getText().removeSpan(mSelectionForegroundColorSpan);
    }
    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

enter image description here

Will Li
  • 495
  • 1
  • 3
  • 12
0

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="http://schemas.android.com/apk/res/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" />
</selector>

And then set your textColor property to @drawable/selector_name

Community
  • 1
  • 1
Chase
  • 11,161
  • 8
  • 42
  • 39