How to set ListView selected item alternet text color in android

11,205

Solution 1

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

Solution 2

Like others have said, just change the color in your onItemClick method. Additionally:

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);


        setItemNormal();
        View rowView = view;
        setItemSelected(rowView);

    }

public void setItemSelected(View view){
    View rowView = view;
    TextView tv = (TextView)rowView.findViewById(android.R.id.text1);
    tv.setTextColor(Color.BLUE);
}

public void setItemNormal()
{
    for (int i=0; i< mDrawerList.getChildCount(); i++)
    {
        View v = mDrawerList.getChildAt(i);
        //TextView txtview = ((TextView) v.findViewById(R.id.menurow_title));
        TextView txtview = ((TextView)v.findViewById(android.R.id.text1));
        txtview.setTextColor(Color.BLACK);
    }
}

by doing this we set all items to normal before we change it on each item press.

Solution 3

For that you've to set OnClickListener for that particular ImageView and then change TextView's color on click of ImageView in Adapter Class.
Alternatively,

mList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            TextView tv= view.findViewById(resID); 
            tv.setTextColor(color)
        }
    });
Share:
11,205
KousiK
Author by

KousiK

Just give a up vote if my answer help you...

Updated on June 05, 2022

Comments

  • KousiK
    KousiK almost 2 years

    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>
    
  • KousiK
    KousiK about 10 years
    but how to change the others item to default color
  • Manuel Escrig
    Manuel Escrig over 9 years
    The following code works for me: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#fff"/> <item android:state_activated="true" android:color="#fff"/> <item android:color="#000" /> </selector>
  • Filip Luchianenco
    Filip Luchianenco over 5 years
    That's a bad practice to have to iterate through all items on every tap. Just use the built in functionality.
  • kbro
    kbro almost 4 years
    the file needs to be in res/color, not res/drawable