androidandroid-listviewcustom-lists

Android - custom listView, each row different colours


I have created custom listview, each row looks like my file custom_row.xml. Is there any way, how to set up different background colors for each row separately (I need to set it because of different values my rows can have).

Thanks for any idea


Solution

  • Since your doing custom listview in the getView method after inflating your custom_row.xml change the background of the return view of inflate method. See sample snippet below:

    public getView(int position, View convertView, ViewGroup parent) {
           convertView = getLayoutInflater().inflate(R.layout.custom_xml, null);
           do some stuff...
    
           //let say you have an arraylist of color
           convertView.setBackgroundColor(arraylist.get(position));
    
           //in case that your color is limited, just re-use your color again
           //and some logic how to re-use the colors.
    }