HI all, i have 10 rows in my listview, and i want to provide different colors in each row. how can i do this???
+2
A:
Change the color of the rows in getView()
. If you are sure to have only and always 10 rows, can simply use a switch on the position of in the adapter (something like that) :
switch(position){
case 0:
yourView.setBackgroundColor(R.color.blue);
break;
case 1:
yourView.setBackgroundColor(R.color.green);
break;
//... and so on until 9.
But if you might have more rows than 10, this is a bit clumpsy because after the 10th row, you'll get views without any background. So you could replace de switch by a serie of if/else if and use the % (modulo) to make row the same color every 10 rows. Does this help?
Sephy
2010-08-10 09:17:50
thanks sephy,yea i m confirmed about the number of rows.What is the funda of R.color.blue???how to define that?
shishir.bobby
2010-08-10 09:21:41
well, you need to define a colors.xml file in your value folder and you add colors you want to use in it like this for instance : `<color name="blue">#3333FF</color>` then the R.color.blue will render you the blue for this hexa
Sephy
2010-08-10 09:41:44
i was trying something like thiscase 0: convertView.setBackgroundColor(0xff000021 + 0xff0021); break; case 1: convertView.setBackgroundColor(0xff000000 + 0xff0000); break; case 2: convertView.setBackgroundColor(0xff000000 + 0xff0000); break;but not able to give hexa code for colors....plz suggest
shishir.bobby
2010-08-10 09:59:00
Are you sure you can add hexa color references?(maybe, but I've never seen that...
Sephy
2010-08-10 10:36:09
you can't pass an hexa reference directly like that, you need to parse it or use a method to convert it : `Color.parseColor("#AARRGGBB")` or `Color.argb(a_int, r_int, g_int, b_int)`
Sephy
2010-08-10 10:39:19
yea man, finally got it,, its running absolutely f9.thanks again
shishir.bobby
2010-08-10 10:45:32
Hey Sephyi was using sampleTextview.setBackgroundResource(R.color.a1);this to set background of textview,which is working great.How can i change textview's font color. bcoz "setBackgroundResource" this works for textview's background.how can i change font color ?
shishir.bobby
2010-08-10 12:41:43
you should take time to read the documentation, it is all explained in it. If you have new questions, don't hesitate to search on stackoverflow and you will probably find your answer quickly than asking a new question. textview text color : http://developer.android.com/reference/android/widget/TextView.html#setTextColor%28android.content.res.ColorStateList%29
Sephy
2010-08-10 14:28:01
i got it working man..thanks a lot
shishir.bobby
2010-08-11 12:37:04