Thursday, May 19, 2011

Problem with textColor by using selector

Recently I became the problem using selector as background image and textColor.

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

Without setting textColor value the text color wasn't that I wanted to have (WOW, surprise, surprise!!!).

And by setting textColor as a color value selector image didn't work anymore.

I couldn't explain my problem, so I've asked by StackOverflow, this time without success after several days.

But it's nice to have such friends like Google (as Germans say, Google is your Friend - Google ist dein Freund). And Google has found the answer for me.

Guess where? Yes, on StackOverflow :)

And here is the solution. Everything works fine if you'll set as text color another selector (in my case selector_icon_text_color.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:color="#777777" />
<item
android:state_pressed="true"
android:color="#AAAAAA" />
</selector>

And so modificated code for my TextView looks like:
<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:textColor="@drawable/selector_icon_text_color"
android:background="@android:drawable/list_selector_background"/>

Thank you Google, thank you Stackoverflow.