-11

I have a list with items, and 2 colorliststates, one for the odds, one for the evens. basically, it ressembles this :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:color="#fff" />
    <item android:state_pressed="true" android:color="#999" />
</selector>

How can I use apply the colorliststate to the items? in the layout, there is no notion of odd or even, so i can't. In the java code, I can't find a way of using the colorliststate once i load it :

getContext().getResources().getColorStateList(R.color.list_even);

Any idea ?

Léon Pelletier
  • 2,490
  • 2
  • 34
  • 64
njzk2
  • 37,030
  • 6
  • 63
  • 102
  • Actually, the solution seems to define the colors in a values xml file, and then use these in xml drawables selectors – njzk2 Aug 10 '11 at 15:26
  • my understanding is the answer basically is this http://stackoverflow.com/questions/3592780/selector-on-background-color-of-textview – njzk2 Feb 25 '13 at 16:35

2 Answers2

0

First define two different selector drawables. You can refer in the drawable xml to a color.xml or enter directly the color value. To use the color.xml is more fancy.

Second I guess you have ListAdapter implementation. So in your getView(..) implementation you can set different background drawables for odd and even items. I guess you want to modify the background and not the text color because you posted an example referring to a pressed state.

Expressed in code, without having eclipse here and totally correct Syntax in mind:

if (position % 2 == 0) {
  view.setBackGroundDrawable(R.drawable.selector1);
}
else {
  view.setBackgroundDrawable(R.drawable.selector2);
}

Does my example match what you want?

Diego Frehner
  • 2,149
  • 1
  • 21
  • 31
  • not entirely. I figure the setBackgroundDrawable part, but i don't see what to do with a colorListState. – njzk2 Dec 14 '12 at 08:56
  • you mean i should first define the colors, and then use them as drawables in a selector ? – njzk2 Dec 14 '12 at 10:05
  • I mean that you don't need a selector. I think you want to adjust the color by the even/odd state, which can not be reflected by a selector. You need to set the desired color in the list adapter. Maybe you can adjust your question to clarify what you'd like to achieve. – Diego Frehner Dec 14 '12 at 10:08
  • as stated in the question, there is also the matter of pressed/not pressed that changes the color (which, far as i know is solved using a state list selector) – njzk2 Dec 14 '12 at 10:20
  • Ok, think i got what you mean. I edited the question. Does it help? – Diego Frehner Dec 14 '12 at 10:42
0

You can use:

// you must place your file in the res/color folder
widget.setTextColor(getContext().getResources().getColor(R.color.list_even));

Or just put it in the textColor attribute if you are using XML:

<SomWidget
    android:textColor="@color/list_even"/>
Cristian
  • 191,931
  • 60
  • 351
  • 260
  • that does not seems to work with background, actually. it keeps saying there is no drawable attribute – njzk2 Jun 17 '11 at 16:23