xRobot
xRobot

Reputation: 26567

ListView: Why doesn't the background change the first time?

I need a way to change the background color of selected items. The code below change the background color ( blue ) only when I click an item for the second time. So when I click an item the first time, it doesn't work.

final SongAdapter songAdt = new SongAdapter(getApplicationContext(), songList);
lv.setAdapter(songAdt);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i(TAG, " executed");
            view.setBackgroundColor(Color.BLUE);
        }
    }
);

I have just checked ( by using the Log.i function ) that the code is executed 2 times, but only the second time the background is changed. Why ?

Upvotes: 2

Views: 2451

Answers (3)

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this:

private int prevPosition=-1;

and your onItemClick use:

lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapter, View view,
            int position, long arg3) {

        for (int i = 0; i < adapter.getChildCount(); i++) {
            if (i == position) {   

                if(position!=prevPosition){
                   //set your selected color                    
                   adapter.getChildAt(i).setBackgroundColor(Color.BLUE);
                    prevPosition=position;

                }else{
                    adapter.getChildAt(i).setBackgroundColor(Color.BLACK);
                    prevPosition=-1;
                   }

               }else{
                  //set your normal color   
                  adapter.getChildAt(i).setBackgroundColor(Color.BLACK);

               }

            }
    }

});

Option 2

you can use drawable selector for your listview

in res/drawable folder

background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
    android:drawable="@drawable/pressed" />
<item  android:state_focused="false" 
    android:drawable="@drawable/normal" />
</selector>

pressed.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#000000"/>  // set your selected color   
<padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
</shape>

normal.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>    

<padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
</shape>

Now use it in your listview in xml:

android:listSelector="@drawable/background" 

Upvotes: 1

Manohar
Manohar

Reputation: 23404

Remove the Line

view.setBackgroundColor(Color.BLUE);

and add this line and see if working

parent.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.list_blue_colour));

i have not tested it yet, please share if working, if not working then follow this answer over here

Upvotes: 0

Ekta Dushanj
Ekta Dushanj

Reputation: 119

In listview, we have property android:listSelector="@color/sky" use this in xml.This will change selected item background.Hope this works for you.

Upvotes: 0

Related Questions