Jeeten Parmar
Jeeten Parmar

Reputation: 5787

Change Custom Listview Item Background Color in Android

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;
                }
            };

Upvotes: 0

Views: 2113

Answers (4)

Akshay
Akshay

Reputation: 6142

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..

Upvotes: 0

Jeeten Parmar
Jeeten Parmar

Reputation: 5787

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.

Upvotes: 1

Dehan Wjiesekara
Dehan Wjiesekara

Reputation: 3182

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;
            }
        };

Upvotes: 0

Anand Phadke
Anand Phadke

Reputation: 531

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.

Upvotes: 0

Related Questions