user2199280
user2199280

Reputation: 347

How to Change background color of list item in Android?

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

Upvotes: 3

Views: 5362

Answers (3)

SathishKumar
SathishKumar

Reputation: 1644

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;
            }
        });

Upvotes: 2

michoprogrammer
michoprogrammer

Reputation: 1199

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!

Upvotes: 3

Linga
Linga

Reputation: 10573

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);

Upvotes: 0

Related Questions