Nitin Bathija
Nitin Bathija

Reputation: 810

Change the font color of TextView when ListView Item gets selected

There is a TextView in every ListView Item which I am setting through custom adapter. The TextView XML is not in same file where a ListView XML has been written, I want that when any Item of ListView gets selected the font color of that particular item should change. I also tried this by defining the different states of TextView i.e selected, focused and pressed but that dose not solve my problem. Please suggest me some solutions for it. Here is snippet..

a listeview in one xml file for eg. file1.xml

<ListView
    android:id="@+id/listView1"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="0.5"
    android:clickable="true" />

and a TextView in different xml.. i.e file2.xml

<TextView
    android:id="@+id/rowListTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:text="@string/app_name"
    android:textColor="@color/file3"
    android:textSize="10sp"
    android:textStyle="bold" />  

file for text color attribute in res/color folder i.e file3.xml.

<item android:state_selected="true" android:color="@android:color/white"/>
<item android:state_focused="true"  android:color="@android:color/white"/>
<item android:state_pressed="true"  android:color="@android:color/white"/>
<item android:color="@android:color/black"/>

Upvotes: 8

Views: 14936

Answers (3)

Vipul
Vipul

Reputation: 28093

Below snippet will help you.

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView <? > adapterView, View rowView,
    int position, long id) {
        TextView textView = (TextView) rowView.findViewById(R.id.rowListTextView);

        textView.setTextColor("Desired Color");

    }
});

I have not included scenario like retaining text color when you scroll the list and your selected item goes out of focus. This snippet will guide you in the right direction.

Upvotes: 7

Master
Master

Reputation: 683

In selector:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item  android:state_activated="true"  android:color="#ff0000" />
    <item android:state_active="true" android:color="#ff0000" />  
    <item android:state_pressed="true" android:color="#ff0000" />
    <item android:state_selected="true" android:color="#ff0000" />
    <item android:state_focused="true" android:color="#ff0000" />
    <item android:color="#000000" />
</selector>

In activity: myListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {
                     pl=position;
                     myListView.setItemChecked(pl, true);
                     adapter.notifyDataSetChanged();
                          .....

item.xml:

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:textColor="@drawable/selector"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    />

And my checked item in red color after i click them. Only one checked item. Enjoy!

Upvotes: 14

teerapap
teerapap

Reputation: 5423

Try this color state list for textColor.

<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>

Android guide does not mention state_activated attribute but it works for me.

Upvotes: 22

Related Questions