Search code examples
androidlistviewandroid-listviewonclickposition

Android listview item background change


i have a android listview. i want to change listview item background when i click one listview item.

and then previous selected item must go back to default background. this means only one item has to be selected.

i have searched it for a long time. i can change background of selected item using onItemClick()

but i can't change previous selected item. for example, if i select second item, it was changed. and then i select third item. oh my god! it is changed too! what can i do for this. how can i get the previous position?

here is my android code.

private class ListViewItemClickListener implements
            AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            TextView title = (TextView) view.findViewById(R.id.title);
            title.setBackgroundResource(R.drawable.list_shape);

        }
    }

Solution

  • When I have this in a similar example I have a global field named:

    selectedListItem;
    

    This would be updated in your onitemClickListener and the previous item would then have it's background returned to the default.

    So to update your code:

    private class ListViewItemClickListener implements
            AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            //First update the previously selected item if one has been set
            if(selectedListItem!=null){
                TextView previousTitle = (TextView) selectedListItem.findViewById(R.id.title);
                previousTitle.setBackgroundResource(R.drawable.list_default_background);
            }
            //Then update the new one
            TextView title = (TextView) view.findViewById(R.id.title);
            title.setBackgroundResource(R.drawable.list_shape);
            selectedListItem = view;
    
        }
    }
    

    So simply initalise selectedListItem as a field in your adapter with the onClickListener as an inner class and have your default background drawable instead of list_default_background .

    Alternatively you can track the position numbers instead of the actual view.

    EDIT:

    To use this method for your list you will also have to keep track of an ID or object instance for your specific list item. In my own solution, in my ListAdapter's getView method I make sure only the list item that matches the ID/instance of the correct item is updated. With your code the way it is you will also find that when you scroll down the view at the same position in this list of visible items is also updated. This is because list view's refer to the list in sets of items, where each set corresponds to the items visible on the screen at any one time.

    To update a singular, specific item you would be better suited to using a selector background or indicator as mentioned in the other answers.

    HTH