This is for everyone trying:
- Programmatically select an item in a ListView
- The stay of this stay element is highlighted
I am working on Android ICS, I do not know if it works at all levels of Api.
First create a listview (or get one if you are already in the Activity / listFragment list)
Then set your list selection mode to single using: Mylistview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Then programmatically select your item using Mylistview.setItemChecked(position, true); (the position is an integer indicating the rank of the item to select)
Now your item is actually selected, but you can see absolutely nothing, because there is no visual feedback of the choice. Now you have two options: you can use a pre-built list or your own list.
1) If you want to create a pre-created list, try simple_list_item_activated_1 , simple_list_item_checked , simple_list_item_single_choice , etc.
You can customize your list this way, for example: setListAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_activated_1, data))
after which you have selected a pre-configured list, you will see that when you select the checkbox, the checkbox is checked or the background has changed, etc.
2) If you use a custom list, you will define a custom layout that will be used in each element. In this XML layout, you assign a selector to each type of part in the row that you want to change when you select it.
Suppose that when you select, you want your line to change the text color and background color. Your XML layout can be written as follows:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/menu_item_background_selector" android:orientation="horizontal" > <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:textColor="@drawable/menu_item_text_selector" />
Now in the drop-down folder you will create menu_item_background_selector.xml and menu_item_text_selector.xml.
menu_item_text_selector.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_activated="true" android:color="#FFF"> </item> <item android:state_pressed="true" android:color="#FFF"> </item> <item android:state_pressed="false" android:color="#000"> </item> </selector>
When you select text, the text will be white.
Then do something similar for your background: (remember that you are not forced to use color, but you can also use drawables)
menu_item_background_selector.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_activated="true" android:color="#0094CE"> </item> <item android:state_pressed="true" android:color="#0094CE"> </item> <item android:state_pressed="false" android:color="#ACD52B"> </item> </selector>
Here the blue background, if selected, and green, if not selected.
The main element that I was missing was android:state_activated . There really are (also) many states: activated, pressed, focused, checked, selected ...
I'm not sure if the example I gave with android:state_activated and android:state_pressed is the best and cleanest, but it seems to work for me.
But I didn't need to create my own class to get Custom CheckableRelativeLayout (which was dirty and scary), and I did not use CheckableTextViews. I donβt know why others used such methods, maybe it depends on the level of Api.