Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 910
Nitin Bathija Avatar asked Nov 22 '12 06:11

Nitin Bathija


People also ask

How to change Text color of selected Item in ListView in Android?

Forms ListView, you can change the text color of selected item by using SelectionChanging event. TextColor updated in SelectionChanging event, based on selection added or removed. LabelTextColor bound to the Label added to SfListView in the ItemTemplate and Behaviour.


2 Answers

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.

like image 188
teerapap Avatar answered Oct 25 '22 23:10

teerapap


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!

like image 44
Master Avatar answered Oct 25 '22 23:10

Master