Change ListView text color on click

I configured ListView that each line of the list consists of ImageView and two TextView . I want to change the color of the text to white when the list item is clicked (only in a click). In addition, I want to change the color to black when the element is "unclicked" (when the "click" is released). I already changed the background color of the item when it was list_item_bg.xml 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 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 text to change the color, exactly the same as the background, as shown in the above xmls. I would prefer to make this change code if possible ...

+4
source share
1 answer

Create a new StateListDrawable, as before, but with black by default and white when clicked.

 <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 in the TextView, change the text color to a new drawable:

 android:textColor="@color/list_item_text" 

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

+19
source

All Articles