0

I want to change Custom Listview's each Item Background Color. So with Adapter, I use getView() but It is not working. How to do this ?

My Code is as below :

adapter = new SimpleAdapter(this, aaReportData, R.layout.report_card1, new String[] { "Topic" }, new int[] { R.id.tvTopic}) {

                @Override
                public View getView(int position, View convertView,
                        ViewGroup parent) {
                    // TODO Auto-generated method stub
                    convertView.setBackgroundColor(R.color.lightish);
                    return convertView;
                }
            };
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111

4 Answers4

1

As per @Dhaval said, I defined background color to parent layout and It worked fine. Now, I want some space between each List Item so I used Drawable and set color and border bottom as white for some space.

It is working fine now and I got result as per my requirement. Thank you so much all of you.

Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111
0

you should use setBackgroundResource() method instead of setBackgroundColor()

tryout following code

view.setBackgroundResource(R.color.blue)
adapter = new SimpleAdapter(this, aaReportData, R.layout.report_card1, new String[] { "Topic" }, new int[] { R.id.tvTopic}) {

            @Override
            public View getView(int position, View convertView,
                    ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                // TODO Auto-generated method stub
                v.setBackgroundResource(R.color.lightish);
                return convertView;
            }
        };
Dehan Wjiesekara
  • 3,152
  • 3
  • 32
  • 46
0
convertView = View.inflate(mContext, R.layout.list_item, null);
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.lightish));

You should inflate list item view use above line to set color to view regards.

Anand Phadke
  • 531
  • 3
  • 28
0

Use this selector xml in you ListView:

<item>
<shape android:shape="rectangle">
   <gradient
       android:startColor="@android:color/holo_orange_light"
       android:endColor="@android:color/holo_red_light"
       android:angle="270"/>
   </shape>
</item>

Updated:

Make changes in your ListView like:

    <ListView
    android:id="@+id/listView_contact"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="0.3dp"
    android:background="@android:color/transparent"
    android:listSelector="@drawable/productlistselector">
</ListView>

You can use divider and set its background transparent instead of designing new custom Background..

Akshay
  • 6,029
  • 7
  • 40
  • 59