Search code examples
androidselectsetbackground

Highlight current touched item as selected item


I have a android file explorer application and I need to highlight the selected item. I could select touched items using v.setBackgroundColor() method inside onListItemClick(). But when I touch another file/folder still previous one highlighted. I need only current touched item display as selected. How to do this?


Solution

  • not perfect but works good

    loop through all items of list and try something like this

    list.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
    
                for (int i = 0; i < arg0.getCount(); i++) {
    
                    View view = arg0.getChildAt(i);
                    if (i == arg2) {
                        view.setBackgroundColor(Color.RED);
    
                    } else {
    
                        view.setBackgroundColor(Color.GREEN);
    
                    }
                }
    
            }
        });