We have talked about how to make a ListView in Android Application using SimpleAdapter.
If you want to add different colors to the background of each row, you can create another adapter which extends the SimpleAdapter and overrides the getView() method. For example, create the SpecialAdapter.java as follow.
import java.util.HashMap; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.SimpleAdapter; public class SpecialAdapter extends SimpleAdapter { private int[] colors = new int[] { 0x30FF0000, 0x300000FF }; public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) { super(context, items, resource, from, to); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); int colorPos = position % colors.length; view.setBackgroundColor(colors[colorPos]); return view; } }
Instead of calling SimpleAdapter in your application, you should use the SpecialAdapter.
... /* Use SpecialAdapter instead of SimpleAdapter SimpleAdapter adapter = new SimpleAdapter(this,fillMaps,R.layout.grid_item,from,to); */ SpecialAdapter adapter = new SpecialAdapter(this,fillMaps,R.layout.grid_item,from,to); ...
Your final ListView will look like this
You can add more colors in the colors array inside the SpecialAdapter.java if your want to create a more colorful ListView.
Done =)
Reference: Creating a ListView with Alternating Colors in Android
Thanks so much! Very straightforward and works perfectly. Exactly what I needed for my project.
LikeLike
You are welcome. =)
LikeLike
So brilliant and so very simple. Clear and precise and easy to follow. Well done.
Added it to my project, so now my listview rows have some “punch” to them. Makes reading the rows nicer 😀
Thank you and keep up the good work.
LikeLike
Glad to know that my post can help you. Thanks for your encouragement. =)
LikeLike
I hava a problem with this,if you get more items (for example 50)to show, and then how to implement this view (request selected color with seleted item). Need you help,thank you.
xm form china
LikeLike
Hi Xianming,
Do you mean u want to apply different colors on different rows instead of repeating the same colour sequence?
Kit
LikeLike
The trouble is that: I want to show the selected item with a different color ,and when the item become not selected ,it goes to former color.I have already achieved it when all items are in one screen,but when the items go out of one screen,the selected item become not selected ,it cannot goes back to its former color. My English is a bit poor,I hope I have expressed myself clearly.
LikeLike
i see.
i haven’t tried this before. let me update you later after i have tried it at home.
LikeLike
OK,thanks a lot!
LikeLike
Hi Xianming,
i just realized that my code doesn’t contain the “Colour change” logic.
could u tell me how you implement it?
Kit
LikeLike
you can see my problem and code at this:[http://www.eoeandroid.com/thread-20110-1-1.html].
LikeLike
i just take a look on your source but i have no clue on the cause of the problem.
have you ever tried to run in debug mode and see if the ResetColor(View v, int positionPre, int position) function was run probably when there were too many rows?
LikeLike
Yes,the problem is just involved in the parameter :positionPre. when there are too many rows, row returned by the code[mviewPre = parent.getChildAt(positionPre)] will be not the former selected row;for example,suppose that one screen contains 11 rows,when selected rows goes to 15 rows,the positionPre will be 14(supose going down),but the code[mviewPre = parent.getChildAt(positionPre)]returns 14th row according to current screen,so mviewPre is if fact out of current screen(because that one screen contains 11 rows).So,ResetColor(View v, int positionPre, int position) become nonsense.
Therefor, I think if there are other solutions, or can solve the problem with [mviewPre = parent.getChildAt(positionPre)].
LikeLike
Hi ykyuen
Problem solved,Thank you.[http://www.eoeandroid.com/thread-20110-1-1.html].
LikeLike
Good to know that you have solved the problem =)
LikeLike
Hi,
Nice Article. 🙂
In my app i need to show a view with 2 textview & 1 image.
For that view i need to set alternative background color.
can you please help how to set it ?
LikeLike
you can follow this article Android – Simple ListView using SimpleAdapter to create a simple listview first. and then configure the layout with 2 TextView and 1 ImageView.
After that you could use SpecialAdpater with alternate color to replace the SimpleAdapter.
Hope this help =)
LikeLike
Hey i know this…
I need to pass 2 textview & 1 image.
How to pass three items in a List ( that is HashMap )…
You have any idea ?…
LikeLike
The ArrayList size equals to the number of row in the ListView. and the number of items in the HashMap contains the items for each row.
You can put items into the HashMap as many as you want. you just need a key to retrieve it.
LikeLike
Thanks for this.
LikeLike
You are welcome =)
LikeLike
Can you please give an example for this.?..
When we are extending with SimpleAdapter the default constructor will be
public sample(Context context, List<? extends Map> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
In this List<? extends Map> data they asked only for two one for String and another may be object… Then how can we use more then 2 items…?
LikeLike
you can try to use List<HashMap<String, Object>> to replace the List<? extends Map>
Then prepare an ArrayList
LikeLike
Thanks 🙂 Let me try and the you know whether it works or not
LikeLike
Hi,
I tried to use a similar thing, when having only two colors, by simply doing:
if (position % 2==0)
view.setBackgroundColor(colors[0]);
else
view.setBackgroundColor(colors[1]);
which is exactly the same. But I have a problem with that: if you have a lot of elements in the list, and that you don’t see some of the elements, then when scrolling down and up again, because Android doesn’t load every element directly but loads only those that you see, then the position will be reset to 0 more than once (can be every time it adds a row if you scroll very slowly). As a result, by scrolling slowly, every row on the top (by scrolling up) will have the position 0, and so the same color.
Does anybody have the same problem? I am using an EfficientAdapter to display my list.
LikeLike
you try to use “parent.getFirstVisiblePosition()” to get the position of first row in the view that you can see.
LikeLike
LikeLike
Hi,
Thanks for the code. however, I have a problem when scrolling the list the selected position is not highlighted. So there is no way of knowing what index is currently selected. Any thoughts?
Thank you.
Syd
LikeLike
Hi Syd,
you can set the OnItemClickListener to the ListView and you should be able to get the position of the item u clicked. you can add the following piece of code in my example.
Regards,
Kit
LikeLike
hi ykyuen
its a great job you did , realy very usefull …
I added the code above , but I got these problems,
1-
code:
OnItemClickListener itemClickedListener = new OnItemClickListener() {
error:
The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)
2-
code :
public void onItemClick(AdapterView parent, View view, int position,
error:
View cannot be resolved to a type
Marco , from Brazil
LikeLike
ohhh ,
I fixed the problem,
thanks anyway
[]´s
LikeLike
you are welcome. =)
LikeLike
Hi Ykyuen,
Sorry i did not word my problem correctly. Actually i do know which position is selected but on the ui the list item is not highlited. It is just a ui issue.
Thanks,
Syed
LikeLike
Hi Syd,
it seems that this ListView behaviour has no way to fix. do you find anyway to solve the problem.
Kit
LikeLike
thanks, good and simple (:
LikeLike
Thanks =)
LikeLike
Thank you for taking the time to post your samples! Worked great.
LikeLike
glad to know that the examples could help you =)
LikeLike
Thanks very much, your code example is very straightforward and readable. VERY HELPFUL! 🙂
LikeLike
good to know that it helps =)
LikeLike
Thanks buddy.. works liked charm .. 😛
LikeLike
Not at all
i just follow the reference. =)
LikeLike
Great work.
When I used following colors the focus is not working perfectly
private int[] colors = new int[] { 0xFFFFFFFF, 0xFFE7F6E6 };
can u please help. what is the problem
LikeLike
Hi Harikrishnan,
sorry for the late reply. i also found that when i click the row of the list view on the simulator, sometimes the focus is lost. and i found that this problem is not caused by the colors. and when i used the keyboard arrow button to select the row, the problem would not appear.
have you tried to deploy it to a android phone?
LikeLike
hey, me again x)
how can i change textstyle, textcolor ….
i haven’t got text options on view
LikeLike
try this
LikeLike
it works thanks!
i have no idea why this doesn’t work in main.java. if i call this there, it throws me nullpointerexception.
sorry for such stupid question =)
LikeLike
great =)
LikeLike
Nice example dude and i think this will too help you http://android-codes-examples.blogspot.com/2011/03/customized-listview-items-selection.html
LikeLike
Thanks for the link. =)
LikeLike
Hi, I want to know how to change the color of the text. I don’t want to change all the text, only a item. Thanks.
LikeLike
This post should help
Android ListView Text Color
LikeLike
Great work.. it helps me a lot.
LikeLike
Great =D
LikeLike
hi
i set the odd and even row color as you told but when i change the color value it didn’t reflat in the list so please help me in this my line is as below
private int[] colors = new int[] { 0xE1E2E3, 0xD2D6D8 };
so how to add add this
LikeLike
the color code should be 8 digits. pls refer to Color | Android Developers
LikeLike
Thanks for your replay as you mentioned i refer the Color |Android Developers and now its working perfectly fine.
LikeLike
Great~ =D
LikeLike
Hi
I am facing one problem can u please solve it
How to fill the different color at rectangle gridview using java(android)
please solve my problem
thank u
LikeLike
try android:listSelector=”@color/red” in your layout xml.
Reference: Android – GridView – 1001 attributes to keep in mind
LikeLike
Hi thank u for gave me this information. but i need one more i want fill 9*9 rectangle gird with different colors( each rectangle with different colors) using java(android code) please tell me
LikeLike
Hi kumar,
i can’t give u much more information since i haven’t touched Android development for more than one year.
For each gridview, you can set the number of columns with the layout xml or setNumColumns().
To fix the number of rows, just make sure u put 81 items to the adapter and set it to the grid view object.
For different colors per each item, it could be set by layout xml of the items inside the adapter. If you really cannot make it, i suggest you could follow the HelloGridView example.
good luck~
Kit
LikeLike
Thanx for referencing this helpful article. 🙂
LikeLike
You are welcome, Paresh. =)
LikeLike
Update this link: http://www.technotalkative.com/android-gridview-1001-attributes-to-keep-in-mind/
LikeLike
updated. =)
LikeLike
Thanx for updating 🙂
LikeLike
Hi
I am new to android. can u please tell me , I want to create board having 8*8 grid with different colors in each square and placing one coin on alternative squares with moves in Horizontal and vertical and diagonal direction similar to checkers game so please reply me
LikeLike
Please refer to the above comment.
LikeLike
Thank u
LikeLike
Hi ykyuen. I am facing one problem. I have spinner having 5 items(suppose item1, item2, item3, item4…). I want to change the textcolor of the selected item. For example item1 in red, item2 in blue, item3 in green, item4 in black ….).
LikeLike
You can refer to this comment.
LikeLike
hi,
Thanks for your sample code
Instead using seperate class of SimpleApadter, can we overide the getView() method from the activity, is it possible..?
LikeLike
hi Kumar,
the Activity class does not have the getView() method.
LikeLike
Nice Tutorial …..thank you …….
LikeLike
you are welcome.
LikeLike
Nice!!
LikeLike
Thanks for your comment and good to know this post is helpful to you. =)
LikeLike
Hi, nice article such as the one with the SimpleAdapter. Help me a lot. I´m a beginner, and I try to use the Simple Adapter with colors but, the colour it´s only filled when the user click on the Item of the List.
Works fine but I have some problems. When the user click on another item, the previous selected item got to lost the colour that he has previously.
I try to do this:
without the loop teh item has the colour changed, but when I apply the loop nothing happens with any item.
Do you have any hints for me?
Thanks a lot.
LikeLike
Try this
does it work?
LikeLike
Not really, continue to fill all itens and did not reseted the colours….but it´s a start, I on my way to resolve it and post here.
Tks a lot!
LikeLike
ok~ hope you can fix the problem soon. =)
LikeLike
Hello and thanks in advance. My question is simply how would I changed the background color (or text color) of the first 6 characters of every cell in my listview ?
(they contain the date. Example “Jan 01: Blah blah blah data data… ” )
Any guidance would be appreciated. thanks again.
LikeLike
I think the following tutorial could help you. =)
Chris Umbel – Rich-Style Formatting of an Android TextView
LikeLike
Thank you looks very helpful. I guess my only confusion is …if I populate my listview with a webservice array like so
will arrayAdapter take a textview ???
Regardless, thanks for the help. you have helped me out.
LikeLike
Hi Ahab,
The android.R.layout.simple_list_item_1 is the standard android sdk layout. you can checkout the layout schema in this link.
The simple_list_item_1.xml contains a TextView.
Reference: StackOverflow – What is “android.R.layout.simple_list_item_1”?
Kit
LikeLike
am gettng one problem in my customlistview i have 3 buttons and one text view while press one button am channging background of perticular position of the list and enabling textview now my problem is begins i.e, when am pressing second button in the same view (position) that changed background moves to last position of the list view ……….plz help me why it is happening ………..?
LikeLike
Is there any changes to the order of items in the fillMaps in the SimpleAdapter?
LikeLike
Just took the code at top of this post and modified it to work with by extending SimpleCursorAdapter for my db based app. The clarity of the example made this very easy to do. thanks
LikeLike
Thanks for your comment. =D
LikeLike
Thanks many, exactly what I was looking for. Thanks a lot for Odd, Even different row colors.
Now I am looking for onClick item color change in list view to understand that this Item is clieked. Please help me with simple solution if possible.
LikeLike
try this
StackOverflow – How to change color of ListView items on focus and on click
LikeLike
great tutorial, thanks a lot.
LikeLike
You are welcome and thanks for your comment. =)
LikeLike
Life saver! Every other tutorial I found was so overcomplicated compared to this! thanks 🙂
LikeLike
Quick question – is there anywhere that has a list of valid colour codes? I’ve tried using hex codes but they don’t seem to work. Thanks in advance
LikeLike
There are all together 8 digits in for the color code. For example, 0x99ABCDEF
The first 2 digits is the opacity, FF is 100% opaque.
The rest are for the RGB color, in the above example R = AB, G = CD, B = EF.
You can get the list of color in the following url.
Set RGB color codes for android and RGB color look up table
LikeLike
Hello and thanks a lot. I have a problem. I want to set color for the only fist row of ListView. But when scrolling the list there are many color row. I know that because the position will be reset. Do you have any solutions?
Here is my code
ToanTK
LikeLike
This post should help.
StackOverflow – How can I change the background color for an specific row properly in a ListView? (Android)
LikeLike
Hi, is it possible to change a textView color in functions of is content?
I try this :
I have a null pointer exeption on the if so i think the textview isn’t tuck and i’ve to take the List<HashMap> items data but idon’t know how to do.
LikeLike
finally got the sollution:
create a TrajetAdapter.java :
LikeLike
Great. good to know that you have solved the problem. =)
LikeLike
Hello,
the problem I have is that I already have an adpter that I’m using :
so I can’t understand how I change this to the new adapter?
thanks,
Marina
LikeLike
Actually it is working , thanks!
but it’s not good enough yet,
I’m trying to insert data into listview. my list view is made from 3 columns and I want to compare the data (grades) in the listview to a number. If the data in one column is bigger than the data in the second column, I want the color of the row to be green and if it’s smaller it will be colored in red.
So I nedd to add a function,
So basiclly I want the list view to change colors- every row in another color? by a specific law.
Can you give me the exmple for this?
the newAdapter is the same?
thanks,
Marina
Thanks for the help,
LikeLike
I think you have to work on the getView() function.
Here is a example where you can get the
LikeLike
hi i am to ask that .i want to change the background color of specific row of listview but i will change the color of that row and other row and on scrolling it will change the order of colored rows….
LikeLike
have you tried the solution on this comment?
LikeLike
Hi,
Can you please tell how to search the listview item with multiple words in editext component of listactivity using simple adapter…..
LikeLike
Android Adding Search Functionality to ListView
LikeLike
Thanks so much! It works for me!
LikeLike
You are welcome and glad to know that the post could help. =)
LikeLike
Perfect! Thanks you !! 🙂
LikeLike
you are welcome~ =D
LikeLike