215

I want a simple TextView to behave the way simple_list_item_1 in a ListView does. Here's the XML:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:gravity="center" android:focusable="true"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:background="@android:drawable/list_selector_background" />

Everything works except for the text color that (expectedly) doesn't change in focused state. How do I make it change to textAppearanceLargeInverse?

Swati Garg
  • 995
  • 1
  • 10
  • 21
yanchenko
  • 56,576
  • 33
  • 147
  • 165
  • In case anyone is looking for the xml file, it's here: http://developer.android.com/resources/samples/Home/res/color/bright_text_dark_focused.html – swinefeaster Aug 12 '10 at 22:29

9 Answers9

446

I got by doing several tests until one worked, so: res/color/button_dark_text.xml

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:color="#000000" /> <!-- pressed -->
     <item android:state_focused="true"
           android:color="#000000" /> <!-- focused -->
     <item android:color="#FFFFFF" /> <!-- default -->
 </selector>

res/layout/view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="EXIT"
       android:textColor="@color/button_dark_text" />
</LinearLayout>
Mark
  • 164
  • 2
  • 9
Klaus Balduino
  • 4,461
  • 2
  • 15
  • 2
  • 8
    without the android:drawable the app crashes with this selector xml – neufuture Sep 18 '11 at 13:34
  • 4
    @neufuture: you need to use `android:textColor="@color/button_dark_text"` – Paul Jun 14 '12 at 05:19
  • 3
    There is a document describing the color state lists: http://developer.android.com/guide/topics/resources/color-list-resource.html – Rick77 Jun 11 '13 at 07:56
  • As curiosity, it also workis when the text, button or any item has been activated, e.g, for list item. – jiahao Jun 10 '14 at 15:16
  • Should the selector really reside under `res/color` folder? – mr5 Apr 21 '17 at 04:29
  • Although hex notation is supported. Please take note hex notation with alpha is not(e.g #10FFFFFF white 50% transparency). Using reference to color resource is better(i.e @color/your_color_resource) in this respect. – ChinLoong Feb 17 '19 at 04:18
  • @mr5 and to all trying to implement this: **Yes** it is important to place the selector under `res/color`. Only there you can add a color (instead of a drawable) to the selector! Otherwise you will get an inflating exception. – Cilenco Jun 19 '20 at 06:07
83

And selector is the answer here as well.

Search for bright_text_dark_focused.xml in the sources, add to your project under res/color directory and then refer from the TextView as

android:textColor="@color/bright_text_dark_focused"
R.daneel.olivaw
  • 2,681
  • 21
  • 31
yanchenko
  • 56,576
  • 33
  • 147
  • 165
  • 5
    I think this has changed to "primary_text_dark_focused.xml". – greg7gkb Dec 21 '10 at 19:52
  • 12
    This just blew my mind. I've been handling things with listeners to change text color all this time. Jaw on floor. Brilliant! – Artem Russakovskii Apr 07 '11 at 01:32
  • Never thought that selectors also work for textColor, super easy. – Drejc Feb 14 '12 at 13:37
  • I tried this with the root Layout of a view that I made for my `ListFragment`, and ended up with a [bunch of errors](http://pastebin.com/zzTkn1L5). What am I doing wrong? – MowDownJoe Apr 03 '13 at 13:55
  • we can't use @android:color/white over there.. displaying content assistance not available when I press Ctrl + space – LOG_TAG Mar 06 '14 at 07:22
  • OMG all these years I've been just so living in the dark! Damn! it's so good! Works like a charm! – sud007 Nov 02 '20 at 09:38
35

In order to make it work on selection in a list view use the following code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#fff"/>
    <item android:state_activated="true" android:color="#fff"/>
    <item android:color="#000" />
</selector>

Apparently the key is state_activated="true" state.

Manuel Escrig
  • 2,825
  • 1
  • 27
  • 36
34

Here's my implementation, which behaves exactly as item in list (at least on 2.3)

res/layout/list_video_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/list_video_footer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:drawable/list_selector_background"
        android:clickable="true"
        android:gravity="center"
        android:minHeight="98px"
        android:text="@string/more"
        android:textColor="@color/bright_text_dark_focused"
        android:textSize="18dp"
        android:textStyle="bold" />

</FrameLayout>

res/color/bright_text_dark_focused.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:color="#444"/>
    <item android:state_focused="true" android:color="#444"/>
    <item android:state_pressed="true" android:color="#444"/>
    <item android:color="#ccc"/>

</selector>
k4dima
  • 6,070
  • 5
  • 41
  • 39
  • This worked well for me, and in my case worked quite well for adjusting the highlighted/unhighlighted colors for my TabHost implementation – oddmeter Dec 04 '15 at 13:59
7

In res/color place a file "text_selector.xml":

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue" android:state_focused="true" />
    <item android:color="@color/blue" android:state_selected="true" />
    <item android:color="@color/green" />
</selector>

Then in TextView use it:

<TextView
    android:id="@+id/value_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text"
    android:textColor="@color/text_selector"
    android:textSize="15sp"
    />

And in code you'll need to set a click listener.

private var isPressed = false

private fun TextView.setListener() {
    this.setOnClickListener { v ->
        run {
            if (isPressed) {
                v.isSelected = false
                v.clearFocus()
            } else {
                v.isSelected = true
                v.requestFocus()
            }
            isPressed = !isPressed
        }
    }
}

override fun onResume() {
    super.onResume()
    textView.setListener()
}

override fun onPause() {
    textView.setOnClickListener(null)
    super.onPause()
}

Sorry if there are errors, I changed a code before publishing and didn't check.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
5

Here is the example of selector. If you use eclipse , it does not suggest something when you click ctrl and space both :/ you must type it.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/btn_default_selected"
    android:state_focused="true"
    android:state_enabled="true"
    android:state_window_focused="true"  />
<item android:drawable="@drawable/btn_default_normal" />

You can look at for reference;

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Günay Gültekin
  • 4,486
  • 8
  • 33
  • 36
2

I always used the above solution without searching more after this. ;-)

However, today I came across something and thought of sharing it. :)

This feature is indeed available from API 1 and is called as ColorStateList, where we can supply a color to various states of Widgets (as we already know).

It is also very well documented, here.

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
2

If using TextViews in tabs this selector definition worked for me (tried Klaus Balduino's but it did not):

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <!--  Active tab -->
  <item
    android:state_selected="true"
    android:state_focused="false"
    android:state_pressed="false"
    android:color="#000000" />

  <!--  Inactive tab -->
  <item
    android:state_selected="false"
    android:state_focused="false"
    android:state_pressed="false"
    android:color="#FFFFFF" />

</selector>
samus
  • 6,102
  • 6
  • 31
  • 69
1

Have you tried setOnFocusChangeListener? Within the handler, you could change the text appearance.

For instance:

TextView text = (TextView)findViewById(R.id.text);
text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            ((TextView)v).setXXXX();
        } else {
            ((TextView)v).setXXXX();
        }
    }
});

You can then apply whatever changes you want when it's focused or not. You can also use the ViewTreeObserver to listen for Global focus changes.

For instance:

View all = findViewById(R.id.id_of_top_level_view_on_layout);
ViewTreeObserver vto = all.getViewTreeObserver();
vto.addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {
    public void onGlobalFocusChanged(
        View oldFocus, View newFocus) {
        // xxxx
    }
});

I hope this helps or gives you ideas.

lilbyrdie
  • 1,887
  • 2
  • 20
  • 24