Highlight selected item in “ListFragment”?

I have posted the same problem a couple of times but it hasn’t yet been resolved. I have a ListFragment and I want to highlight the selected item in the list. I have been given suggestions to use a “selector”. I don’t understand how to use this selector. My ListFragment class is:

// Create an adapter with list of stores and populate the list with
        // values
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, StoreList);

     * (non-Javadoc)
     * Handles the event when an item is clicked on left pane, performs action
     * based on the selection in left pane
     * @see android.app.ListFragment#onListItemClick(android.widget.ListView,
     * android.view.View, int, long)
    public void onListItemClick(ListView l, View v, int position, long id) {
        String selectedStore = (String) getListAdapter().getItem(position);
        DetailFragment fragment = (DetailFragment) getFragmentManager()
        if (fragment != null && fragment.isInLayout()) {
            // passes selectedStore to detail fragment  

            // getItemList(selectedStore);


Using setBackground sets the color permanently, but I want it to go away when another item is selected.
I understand how to use a selector in a ListView but in my case if I haven’t defined any xml for the Listview, then how would I use a “selector”? I am using android.R.layout.simple_list_item_1 which is predefined.

  • Transforming accelerometer's data from device's coordinates to real world coordinates
  • How to retrieve the android sdk version?
  • Intent.migrateExtraStreamToClipData() on a null object reference
  • How to disable/enable all children on LinearLayout in Android
  • Android Studio - Appcompat build fail values-v23/styles_bases.xml
  • How to set Spinner Default by its Value instead of Position?
  • Related posts:

    java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    How to implement Search Bar like gmail app in android?
    What CursorAdapter have I to use?
    How to give percentage values in ObjectAnimator in Android
    Android: Get thumbnail of image on SD card, given Uri of original image
    Unable to resolve target &#039;android-18&#039; error
  • Android 2.3 wifi hotspot API
  • Share to Facebook in android (like twitter)
  • Android: out of memory exception in Gallery
  • What functions or codes require GET_TASKS permission in Android?
  • pjsip 2.5.5 build error
  • Android Gradle Apache HttpClient does not exist?
  • 7 Solutions collect form web for “Highlight selected item in “ListFragment”?”

    The following worked for me for:


    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        android:drawable="@color/red"  />
        android:drawable="@color/red" />
        android:drawable="@color/white" />


    <?xml version="1.0" encoding="utf-8"?>
    <color name="white">#FFFFFF</color>
    <color name="red">#FF0000</color>

    res/layout/menuitem.xml:: (XML for every item in the list)

    <?xml version="1.0" encoding="utf-8"?>
            android:layout_width="fill_parent" />

    Finally, in the ListFragment class, add View previous and add the following code to the onlistitemclick function.. (mentioned in ListFragment: highlight selected row)

    public class MenuListFragment extends ListFragment{
    View previous;
        public void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id); 
           //Logic to highlight selected item

    From your ListView, call setChoiceMode(ListView.CHOICE_MODE_SINGLE). Then, whenever you want to highlight a selected item, call setItemChecked(index, true).

    I tried the same and I didn’t find any good solution.
    What I actually do is using this code to set the listener:

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){

    where the list adapter defines the following public methods

    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = GuiBuilder.createHeroListItemView(heroes.get(position),getContext(),parent,false);
        if(position == selected_pos){
        return rowView;
    public void setSelectedPosition(int selected_pos){
        this.selected_pos = selected_pos;
    public int getSelectedPosition(){
        return selected_pos;

    That is, I change the background of the list item programmatically.
    Moreover to avoid the blinking effect when clicking the list element I don’t define any selector for the pressed state

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true"
            android:drawable="@drawable/list_item_focused_color" />
        <item android:drawable="@drawable/list_item_default_color" />

    This works as intended for me. I didn’t find any better solution since setSelected(true) doesn’t work on list items!

    I wasn’t getting what I wanted so I kept on digging and came up with a “cheap” solution which might not be the best practice but does the job.
    I wanted the item to be highlighted when selected and the color should go away when other item is selected from the listFragment.
    This is what worked for me- I defined a static View V;
    and initialized it V = new View(getActivity());
    Then inside my

    onListItemClick(ListView l, View v, int position, long id)

                V = v;

    This worked very nice for me in the ListFragment, but I think it works only for Android 4.0 and up. I created a simple layout for the list item with android:background="?android:attr/activatedBackgroundIndicator (in case of Android below 4.0 you need to ceate the similar one in drawables):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" >
        android:layout_margin="@dimen/double_view_margin" />

    And then, just create a simple ArrayAdapter using this layout and set the choice mode to single:

    final ArrayAdapter<String> labelsAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_simple_list_item, R.id.list_item_text, labels);

    After that you can hightlight the necessary item with setItemChecked method:

    public void onListItemClick(final ListView l, final View v, final int position, final long id) {
        getListView().setItemChecked(position, true);

    i code

    getListView().setItemChecked(0, true); 



    code block

    Something similar to type-a1pha but with the selectors, once you have the view you can view.setSelected(true) and that would add the style you have defined on your selector.
    you need to remove the flag once the action is performed.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.