Change the font color of TextView when ListView Item gets selected
Asked Answered
R

3

8

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

Raddy answered 22/11, 2012 at 6:5 Comment(1)
teerapap's answer is the best answer - i think it must be accepted.Subchloride
F
7

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.

Frulla answered 22/11, 2012 at 6:18 Comment(2)
Thanks vipul I tried that and it helped somewhere as its changing font color of that particular Item where it is been clicked but now I want that it should retain the original color once the clicked is gone from that particular position. can u help me that way. Thanks.Raddy
And how to change the TextView's color without Click, for example when the ListView is created.Announcement
S
22

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.

Shavian answered 23/10, 2013 at 18:9 Comment(2)
android:state_activated is definitely what you want. Why this ish isn't documented is crazy. I spent about 30 mins trying to get this to work, then I tried this and it works.Subjunction
Is this possible for image's too, I want to switch between two images, I tried instead of android:color to android:src for the image but errors meRotation
L
14

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!

Larghetto answered 27/5, 2014 at 17:53 Comment(1)
in case of element selection in listview, this should be accepted answer. Nice answer @Larghetto , actually.Caseworm
F
7

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.

Frulla answered 22/11, 2012 at 6:18 Comment(2)
Thanks vipul I tried that and it helped somewhere as its changing font color of that particular Item where it is been clicked but now I want that it should retain the original color once the clicked is gone from that particular position. can u help me that way. Thanks.Raddy
And how to change the TextView's color without Click, for example when the ListView is created.Announcement

© 2022 - 2024 — McMap. All rights reserved.