erdomester
erdomester

Reputation: 11829

Textcolor selector not working

I did this before. I copy-pasted. I copy-pasted many other examples from the net. I simply cannot make the textcolor selector work. It sets the default color to the textview, but it won't change if you click on the textview. The settings_selector for the background works fine.

This is the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/settings_selector"
    android:clickable="true"
    android:id="@+id/llRecentChanges"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp">
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/llRecentChanges2"
        android:layout_weight="1"
        android:layout_gravity="center_vertical">
    <TextView
        android:id="@+id/tvAbout"
        android:text="@string/settings_recentchanges"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:textColor="@drawable/settings_selector_txt" >
    </TextView>
    <TextView
        android:id="@+id/tvAbout2"
        android:text="@string/settings_recentchanges2"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@drawable/settings_selector_txt"
        android:textSize="10dp">
    </TextView>
    </LinearLayout>

</LinearLayout>

This is the settings_selector_txt xml:

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

or this

<?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>

or this

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
    <item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
    <item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
    <item android:color="#ffffff" />
</selector>

None of them is working. Putting the selector xml to the color folder is also no solution. Any ideas?

Upvotes: 16

Views: 18712

Answers (5)

SBotirov
SBotirov

Reputation: 14138

I think using ColorStateList best choice to older and latest versions of Android.

int[][] states = new int[][] {
    new int[] { android.R.attr.state_pressed}, // pressed
    new int[] { android.R.attr.state_focused}, // focused
    new int[] {}
};
int[] colors = new int[] {
    getResources().getColor(R.color.green_color), // green
    getResources().getColor(R.color.green_color), // green
    getResources().getColor(R.color.white)  // white
};
ColorStateList list = new ColorStateList(states, colors);
mTextView.setFocusable(true);
mTextView.setClickable(true);
mTextView.setTextColor(list);

Upvotes: 2

Thalis Vilela
Thalis Vilela

Reputation: 345

Make sure all layouts that may be wrapping your TextViews before your "clickable" layout also have duplicateParentState="true", otherwise, the text view won't reach the element state you want.

Upvotes: 1

Devrim
Devrim

Reputation: 15533

No need to set android:clickable="true" to TextView or android:duplicateParentState="true".

Solution
Step-1: Define your color selector under res/color folder(Jose L Ugia's color selector is good for this.)
Step-2: set this color selector to your TextView as android:textColor="@color/m_text_selector" (not as @drawable/*!!!)

Note: If you want to set textColor selector programmatically, you must get your selector as color state list not as a color;

textView.setTextColor(getResources().getColorStateList(R.color.m_text_selector));


That's all.

Upvotes: 30

Jose L Ugia
Jose L Ugia

Reputation: 6250

Make sure your TextView is ready for listening the states you are applying for. For instance, to be able to reach the "state_pressed" your textView should be clickable:

android:clickable="true"

EDIT: There we go. This layout does the job. Note that the View that gathers the click event is the linearLayout, but the TextView reproduces it because of "duplicateParentState" set to true. The color selector would take care of the colors for the different states.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/testLlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:clickable="true" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@drawable/textview_selector"
        android:duplicateParentState="true"
        android:text="TextView" />

</LinearLayout>

And here is the code for the color selector.

<?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>

That should be it.

Upvotes: 39

AliSh
AliSh

Reputation: 10619

You must set this in your code : android:clickable="true" for TextViews

Upvotes: 0

Related Questions