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
1,0731 gold badge11 silver badges21 bronze badges
asked Aug 2, 2009 at 17:18
1

9 Answers 9

447

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
1642 silver badges9 bronze badges
answered Aug 25, 2010 at 12:09
Sign up to request clarification or add additional context in comments.

7 Comments

without the android:drawable the app crashes with this selector xml
@neufuture: you need to use android:textColor="@color/button_dark_text"
There is a document describing the color state lists: developer.android.com/guide/topics/resources/…
As curiosity, it also workis when the text, button or any item has been activated, e.g, for list item. <item android:state_activated="true" android:color="@android:color/white" />
Should the selector really reside under res/color folder?
|
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"
answered Aug 14, 2009 at 21:02

6 Comments

I think this has changed to "primary_text_dark_focused.xml".
This just blew my mind. I've been handling things with listeners to change text color all this time. Jaw on floor. Brilliant!
Never thought that selectors also work for textColor, super easy.
I tried this with the root Layout of a view that I made for my ListFragment, and ended up with a bunch of errors. What am I doing wrong?
we can't use @android:color/white over there.. displaying content assistance not available when I press Ctrl + space
|
36

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.

answered Oct 7, 2014 at 8:34

2 Comments

Don't forget to place this file under res/color folder
This should be made the correct answer. Works 100%
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>
answered Dec 15, 2011 at 23:28

1 Comment

This worked well for me, and in my case worked quite well for adjusting the highlighted/unhighlighted colors for my TabHost implementation
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.

answered Mar 19, 2019 at 8:16

Comments

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

answered Apr 4, 2012 at 8:41

Comments

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.

answered Mar 28, 2014 at 14:50

Comments

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>
answered Jun 13, 2014 at 20:38

Comments

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.

answered Aug 6, 2009 at 13:18

1 Comment

I've gone this way initially but text color didn't change for some reason.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.