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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With