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...
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.
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!
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With