0

I have a ListView on my Android app screen. In the list view I am showing data from an ArrayList. Now the Arraylist has 3 fields: Id, Name and Status. I need to show on the screen, the Id and the Name and according to the status value, which can be 0, 1 or 2 set the background color for the particular row. I am able to get the ListView with the values on the screen, but I cannot seem to find any example where I can set color of rows in an android list view on create. Can someone please help me? I use a SimpleAdaptor to show the values of the list for now. Thank you in advance. :-)

This is my ListView xml::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:id="@+id/relativeLayout"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#FFEBEB"
tools:context=".MyActivity2">

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:choiceMode="singleChoice"
    android:textAlignment="center"/>

</RelativeLayout>

This is the XML which adds the rows::

<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="4dip"
    android:paddingBottom="6dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="left">


    <TextView android:id="@+id/TITLE_CELL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_weight="1"
        android:height="40dp"
        android:textAlignment="center"
        android:padding="10dp"/>

    <TextView android:id="@+id/FROM_CELL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:height="40dp"
        android:textAlignment="center"
        android:padding="10dp"/>

</LinearLayout>
cbandroid
  • 21
  • 1
  • 7
  • 1
    you have to create your custom adapter and set view in your adapter . Then check your status value and set background color. – Divyang Metaliya Sep 10 '14 at 08:29
  • Can you shows the code of your getView in your adapter? – Pedro Oliveira Sep 10 '14 at 08:34
  • You should use BaseAdapter to customize the listview,see example :http://stackoverflow.com/questions/16333754/how-to-customize-listview-using-baseadapter – ben Sep 10 '14 at 08:37
  • Hello,thank you for all your answers. I think I should have mentioned this, I know how to do it with a Custom made adapter, as I mentioned, I just use a SimpleAdaptor and wanted to know if there is any possible way to do this same thing only with the use of a custom adapter? Thank you for your responses, all of them are correct, I wish I could accept them all. – cbandroid Sep 10 '14 at 11:26

3 Answers3

4

You can do this in your Adapter class getView() method. For Example:

@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {       
        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.list_row, parent, false);
        }

        //do your stuff

        if(status.equals(0))
        {
          convertView.setBackgroundColor(Color.RED); // or whatever you want to set color
        }
        else if(status.equals(1))
        {
          convertView.setBackgroundColor(Color.GREEN);
        }
        if(status.equals(2))
        {
          convertView.setBackgroundColor(Color.YELLOW);
        }       

        //do your remaining stuff....

        return convertView;
    } 
XtreemDeveloper
  • 992
  • 8
  • 13
3

If you want some random color codes, try this :

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{       
    if(convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_row, parent, false);
    }

    Random rnd = new Random(); 
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    convertView.setBackgroundColor(color);

    return convertView;
} 
Tejas
  • 585
  • 3
  • 15
0

I believe you will need a custom adapter. Override the getView() method, and set the view background as you want.

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder;
if (convertView == null) {
 // inflate the layout

            convertView = inflater.inflate(R.layout.adapter, parent, false);

            // well set up the ViewHolder
            viewHolder = new ViewHolder(convertView);

            // store the holder with the view.
            convertView.setTag(viewHolder);

        }
        else {
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Item myItem = getItem(position);
        int id = myItem.getMyItemId();
         switch (id) {
            case 1:
            convertView.setBackgroundResource("whatever");
            break;
            case 2:
            convertView.setBackgroundResource("whatever ever");
            break;
            case 3:
            convertView.setBackgroundResource("whatever not");
            break;
            default:
            break;
        return convertView;
}
nightfixed
  • 811
  • 1
  • 10
  • 21