简体   繁体   中英

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

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.

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM