Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change textcolors of Android ListView text on selection

I can't figure out what I am doing wrong...

I have a ListView with a custom layout.xml file. In there, i define a TextView like this

<TextView android:layout_height="wrap_content" 
          android:layout_width="wrap_content"
          android:text="foo"
          android:textColor="@drawable/listitem_textcolor_selector"/>

The listitem_textcolor_selector.xml looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <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>

This kind of works. If I select a row, it will properly change the color of the text to red. The only problem is, that it will not stay red. After a second or so, that color will change back to black.

The main problem here is that the background of the row will change it's color and this color will stay, but the color of the text does not, even though the selector for the list item itself looks identical (expect the colors).

Can anybody tell me what I am missing? Any help is appreciated as I have no idea on how to fix this :)

Thanks

Edit: Maybe I should also point out that I am testing on a Samsung Galaxy Tab 10.1 tablet. I once heard something about "TouchMode" without really knowing weather this will have something to do with my problem...

like image 547
Georg Avatar asked Jan 24 '12 12:01

Georg


3 Answers

In the listitem_textcolor_selector.xml add these states:

        <!-- Activated -->      
<item 
    android:state_activated="true" 
    android:color="#ff0000" />

        <!-- Active -->      
<item 
    android:state_active="true" 
    android:color="#ff0000" />  

After this, the selected item will keep it's color state until something else is selected.

Hope this helped.

like image 181
Ionut Negru Avatar answered Nov 20 '22 06:11

Ionut Negru


I win the problem! 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 23
Master Avatar answered Nov 20 '22 05:11

Master


I got the trick. if you are using a custom xml for your list item,then you can change text color like:

custom_listitem:

<?xml version="1.0" encoding="utf-8"?>     
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="7dip"
        android:paddingBottom="7dip"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="@drawable/listitem_textcolor_selector"
         />

And now you will have to use this xml name to your array adapter which you use to populate your listview.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_listitem, options);
listView.setAdapter(adapter);
like image 1
Hiral Vadodaria Avatar answered Nov 20 '22 05:11

Hiral Vadodaria