How to set ListView selected item alternet text color in android

android, android-listview

Solution

Create following button_text.xml in drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

Change your text view's textColor to:

  <TextView
        android:id="@+id/txt_bell_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/rihanna_love_the_way_lie"
        android:textColor="@color/button_text" //point to that xml
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />

You can read about color state list here

Problem

I have a custom listview with a imageview and an textview. I want when user select a item the textview color should change and all the other textview should remain default. here are my xmls ListView.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listViewBell" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:choiceMode="singleChoice" android:divider="#b5b5b5" android:dividerHeight="1dp" android:listSelector="@drawable/list_selector" /> </LinearLayout> ``` ListViewItems.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dip" > <TextView android:id="@+id/txt_bell_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/rihanna_love_the_way_lie" android:textColor="@color/black" android:textSize="15sp" android:textStyle="bold" android:typeface="sans" /> <ImageView android:id="@+id/list_bell_image" android:layout_width="50dip" android:layout_height="50dip" android:src="@drawable/bronze_bell" /> </RelativeLayout> ```

Original source