1

Whenever a user presses on the listview, it changes its background color to red. However, when i press another item, the old item still retains the background color of red. How to edit it such that only the clicked item has the background color changed.

lvContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
        view.setSelected(true);
        view.setBackgroundColor(Color.RED);

        nameSelected = contactHolderArrayList.get(position).name;
        phoneSelected = contactHolderArrayList.get(position).phone;

        //Anything
    }
});
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
srkmish
  • 49
  • 1
  • 3
  • See this http://stackoverflow.com/questions/16976431/change-background-color-of-selected-item-on-a-listview and http://stackoverflow.com/questions/16189651/android-listview-selected-item-stay-highlighted – Rohit5k2 Feb 04 '16 at 19:50
  • You would need to set the other views in the adapter to their original background colors while also setting the selected one to red. But since you'll need to be manipulating more than one of the list items on every click, you'll likely need to write your own custom Base Adapter to manage your views. – NoChinDeluxe Feb 04 '16 at 19:52

2 Answers2

0

I just implemented a simple counter to keep track of prev and current position and it works! Please comment if anything is wrong. I initialized counter to 0 in oncreate and incremented each time list item is pressed and then included below code in onitemclicklistener

lvContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
            //view.setSelected(true);

            if(counter==0) {
                prevPosition = position;
            }
            else if (counter ==1) {
                currentPosition = position;
            }
            else {
                prevPosition = currentPosition;
                currentPosition = position;
            }
            lvContact.getChildAt(prevPosition).setBackgroundColor(Color.TRANSPARENT);
            view.setBackgroundColor(Color.RED);
            counter++;
srkmish
  • 49
  • 1
  • 3
-1

you need to apply onTouchListener on view inside ListView adapter then need to set new Color on Down action and Old color on Up action

public View getView(int position, View convertView, ViewGroup parent)
{
         view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    // apply new color
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    // apply old color
                }
                return false;
            }
        });
       return view;
}
Rohit Patil
  • 1,068
  • 8
  • 9