2

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 ?

xRobot
  • 25,579
  • 69
  • 184
  • 304
  • Can you elaborate the question by providing the entire code to the listview? – OBX Jan 30 '17 at 17:58
  • Most probably the view does not redraw. Having the full source code would help to see why this happens. – Iakovos Jan 30 '17 at 18:00
  • I have added some code in the above question, what part do you need exactly ? Thanks :) – xRobot Jan 30 '17 at 18:08
  • @jackgu1988 I have added more code above :) – xRobot Jan 30 '17 at 18:41
  • @OBX I have added more code above :) – xRobot Jan 30 '17 at 18:41
  • From what I understand, you are trying to highlight the selected item. Please correct me if I'm wrong. You can do that by `lv.setSelector(SOME_DRAWABLE_RESOURCE);`, where SOME_DRAWABLE_RESOURCE can be a colour that you define in `res/values/colors.xml` in the form `R.color.some_colour` – Iakovos Jan 30 '17 at 18:59
  • did your `Log.i(TAG, " executed");` called during first click? – rafsanahmad007 Jan 30 '17 at 19:02
  • @rafsanahmad007 yes, it's called during the first click – xRobot Jan 30 '17 at 19:11
  • @jackgu1988 Yes I want to highlight the selected item. I have just tryed lv.setSelector(R.color.activated_color); and it does work BUT it's highlighted even if the item is focused without selecting it ( for example if I put my finger on an item and then scroll ). – xRobot Jan 30 '17 at 19:15
  • I just noticed that I also have `android:background="?android:attr/activatedBackgroundIndicator"` on the container (RelativeLayout in my case) XML file for the item layout (defined in the adapter). Please try a combination of this and my previous comment. – Iakovos Jan 30 '17 at 19:23

3 Answers3

1

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" 
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • This might not be efficient for a music player. It will slow things down in big song lists. – Iakovos Jan 30 '17 at 19:27
  • the edit looks fine and something similar has worked for me in the past. However I am not sure if it will work, as the OP said that this didn't work: http://stackoverflow.com/questions/41936272/how-to-highlight-pressed-items-in-a-listview – Iakovos Jan 30 '17 at 19:41
  • 1
    it works ..for me....try it and remove your `view.setBackgroundColor(Color.BLUE);` `view.setSelected(true);` in onItemClick – rafsanahmad007 Jan 30 '17 at 19:50
  • @rafsanahmad007 I have just tryed the method 2 and it doesn't work. Could you tell me why ? – xRobot Jan 30 '17 at 20:11
  • during first click or second click it did not change background? – rafsanahmad007 Jan 30 '17 at 20:15
  • if u are using a custom layout for your listview item your should use `android:background="@drawable/background"` for the root layout ..instead of `android:listSelector="@drawable/background"` – rafsanahmad007 Jan 30 '17 at 20:20
  • @rafsanahmad007 it doesn't change during first, second, third, ... click. I have used android:background and android:listSelector. Seems absurd: it's so difficult to just change the background D: – xRobot Jan 30 '17 at 20:33
  • Maybe because I am using Targed Sdk version 22 ? – xRobot Jan 30 '17 at 20:37
  • no it should work in sdk 22 also,,see this: http://stackoverflow.com/questions/22422196/android-listview-listselector-not-working...you have to add your background drawable for adapter class layout – rafsanahmad007 Jan 30 '17 at 20:40
0

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

Ekta Dushanj
  • 119
  • 2
  • 5
  • I have tryed it as you can see here: http://stackoverflow.com/questions/41936272/how-to-highlight-pressed-items-in-a-listview But it doesn't work and I don't know why. – xRobot Jan 30 '17 at 18:10
  • android:choiceMode="singleChoice" try this in your listview @xRobot – Ekta Dushanj Jan 30 '17 at 18:20
0

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

Community
  • 1
  • 1
Manohar
  • 22,116
  • 9
  • 108
  • 144