4

I have a customized ListView that each row of the list is composed by an ImageView and two TextView's. I want to change the text's color to white when a list's item is clicked (only in the clicked item). Also, I want to change back the color to black when the item is "unclicked" (when the "press" is released). I already made the item's background color change when it was clicked with the following list_item_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
    android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@color/white" />

<item android:state_pressed="true" 
    android:drawable="@color/red_destaques" />

<item android:state_selected="true"
    android:drawable="@color/red_destaques" />
</selector>

And the listitem_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="5dip"
    android:maxHeight="50dip"
    android:adjustViewBounds="true"
    android:background="@color/list_item_bg">

    <ImageView
        android:layout_width="70dip"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:scaleType="fitStart" 
        android:src="@drawable/imagem_padrao_lista" 
        android:id="@+id/listItemLogo">
    </ImageView>

    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/listItemLogo"
        android:layout_toLeftOf="@+id/listItemArrow"
        android:textColor="#000000"
        android:layout_marginLeft="5dip"
        android:adjustViewBounds="true" 
        android:id="@+id/listItemTitle">
    </TextView>

    <TextView
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/listItemLogo"
        android:layout_below="@+id/listItemTitle"
        android:textColor="#333333"
        android:layout_marginLeft="5dip"
        android:adjustViewBounds="true" 
        android:id="@+id/listItemDescription">
    </TextView>
    <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:src="@drawable/setinha_navegacao" android:layout_centerVertical="true" android:id="@+id/listItemArrow"></ImageView>

</RelativeLayout>

I the text to change it's color exactly in the same manner that the background changes as shown in the above xmls. I would prefer doing this change by code if possible...

Alesqui
  • 6,417
  • 5
  • 39
  • 43

1 Answers1

19

Create a new StateListDrawable like you did before but with black as default color and white when pressed.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/black" />
<item android:state_focused="true" android:color="@color/black" />
<item android:state_pressed="true" android:color="@color/black" />
<item android:color="@color/white" />
</selector>

Now for in the TextView change the text color to the new drawable:

android:textColor="@color/list_item_text"

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

b_yng
  • 14,136
  • 6
  • 32
  • 35
  • Not working =/. I just receive an ANR when the app is about to load the `ListView`. I even tried to set the text color as `android:textColor="@color/list_item_bg"`, as this xml is for sure working, and still I got the error message =/ – Alesqui Aug 23 '11 at 20:51
  • 1
    Try this one. I added a case for focused. – b_yng Aug 23 '11 at 21:03
  • 1
    Now it works perfectly guy! Thanks a lot! I thought that it would only change the color when the `TextView` would be touched, but it changed when any place of the row were pressed (exactly as I expected). Do you know any link that describes better the way that the `` behaves? I found only superficial explanations... – Alesqui Aug 24 '11 at 11:23
  • Why doesn't it suggest the android:color parameter when I type? – Indrek Kõue Aug 24 '11 at 15:27
  • 2
    I added a link... and I'm not sure, it might depend on the resource folder that the xml file is saved in. So if you save it in /color then it will suggest android:color and if you save it in /drawable it suggests android:drawable? Just a guess – b_yng Aug 24 '11 at 16:16