3

How to change the background color onlongclick of list item in android, now iam getting the position of the list item onlongclick by using below code.

 listView.setOnItemLongClickListener(new OnItemLongClickListener() {

     public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
             int pos, long id) {
         // TODO Auto-generated method stub

         Log.v("long clicked","pos: " + pos);

         return true;
     }
 }); 

Any help will be appriciated,, thank you

user2199280
  • 347
  • 1
  • 3
  • 12

3 Answers3

3

I found a possible solution in this post: Changing background color of ListView items on Android

    private static int save = -1;

public void onListItemClick(ListView parent, View v, int position, long id) { 

    parent.getChildAt(position).setBackgroundColor(Color.BLUE);

    if (save != -1 && save != position){
        parent.getChildAt(save).setBackgroundColor(Color.BLACK);
    }

    save = position;                

}

Change the method to onItemLongClick().

I hope this help!

Community
  • 1
  • 1
michoprogrammer
  • 1,159
  • 2
  • 18
  • 45
2

Try this,

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view,
                    int arg2, long arg3) {

                view.setBackgroundColor(Color.parseColor("#222222"));
                Log.v("Long Click", "Working");
                return false;
            }
        });
SathishKumar
  • 1,644
  • 1
  • 14
  • 28
0

Try this in your onItemLongClick()

public View view1;// should be declared as global
if (view1 != null) {
    view1.setBackgroundResource(R.color.orange);
}
view1 = v;
v.setBackgroundResource(R.color.transparent_green);
Linga
  • 10,379
  • 10
  • 52
  • 104