5

Can You help me please. I need to change background color of my list view item which is selected manually by setSelection(int pos) function and I need to stay with new color until new setSelection call. I have read some topics of how to do this, but I still have no succes. Thanks!

bukka.wh
  • 913
  • 2
  • 16
  • 29

2 Answers2

8

I've managed to accomplish that by making several selectors for the different states

first put this in your listview

android:listSelector="@drawable/list_selector"

Then create xml files in drawable to control the diferent states

@drawable/list_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>

@drawable/list_item_bg_normal

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
  android:startColor="@color/list_background"
  android:endColor="@color/list_background"
  android:angle="90" />
</shape>

@drawable/list_item_bg_pressed

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="@color/list_background_pressed"
      android:endColor="@color/list_background_pressed"
      android:angle="90" />
</shape>

In your ListView Selection

listView.setOnItemClickListener(new OnItemClickListener() {

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

Don't forget to add list_background_pressed and list_background to your values/color.xml or just set the color manually in each file.

And I Believe that when you use setSelection(int pos) that will automatically uset the layout you've set as selectected.

Hope it helps.

T.V.
  • 793
  • 12
  • 33
  • Thanks! I did as You said, but still have no success(( – bukka.wh Jan 21 '14 at 10:04
  • I've edited my code with setOnItemClickListener method, give it a check. – T.V. Jan 21 '14 at 10:18
  • Thanks! I'll try it! Correct me if I am wrong: function setSelection automatically calls onItemClick method of my list view? – bukka.wh Jan 21 '14 at 10:22
  • Yes, I believe it does. Also check this post if you are not successful http://stackoverflow.com/questions/4800415/android-setselected-in-onitemclick-in-listview – T.V. Jan 21 '14 at 10:46
  • 1
    I did as You said but nothing happens((((. Thanks for link I'll check her. May be it will help)) – bukka.wh Jan 21 '14 at 11:02
0
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (updateview != null) updateview.setBackgroundColor(Color.TRANSPARENT);
        updateview = view;
        view.setBackgroundColor(Color.CYAN);
    }
});
trincot
  • 317,000
  • 35
  • 244
  • 286
nandish
  • 41
  • 4