How can I set different background color for each row in listview?

I want to set different background color in each row of listview ? I used custom adapter for listview. It should be appear when activity loads.static different color row.

  • Android video frame processing
  • Circle pager indicator doesn't look round
  • How to unregister from GCM after uninstalling app
  • What are the best ways to crash Android apps?
  • Android webview won't load my URL but will load others
  • Async task Android execute
  • Related posts:

    Android how to create runtime thumbnail
    Can I get the source code for Android's Google Maps app?
    How does application install size vary on phone?
    Open Google Plus Page Via Intent In Android
    OnClickListener of Child preventing the swipe of Android ViewPager
    FIleObserver and ContentObserver not working in Android Marshmallow
  • Using Dependency Injection with Roboguice?
  • Subscribe to a BLE Gatt notification Android
  • How to make part of the text Bold in android at runtime?
  • How background changes at login in Instagram app?
  • Explanation of Android Xfermode
  • Display UTC date/time into date/time according to the current time-zone
  • 4 Solutions collect form web for “How can I set different background color for each row in listview?”

    As you said that you have use Custom adapter for listview then what you need to do is below.
    in getView method of your adapter you need to set background color of your list row xml’s parent view.

    in getView(...) method

    if (position == 0) {
        view.setBackgroundResource(R.drawable.bg_list_even);
    } else if (position == 1) {
        view.setBackgroundResource(R.drawable.bg_list_odd);
    } else...
    

    Update::

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;
    
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            view = inflater.inflate(R.layout.row, null);
    
            holder = new ViewHolder();
            view.setTag(holder);
    
        } else {
            holder = (ViewHolder) view.getTag();
        }
    
        holder.title = (TextView) view.findViewById(R.id.txttitle);
        holder.description = (TextView) view.findViewById(R.id.txtdesc);
    
        holder.title.setText("Title" + position);
        holder.description.setText("Desc" + position);
    
        //here set your color as per position
    
        if (position == 0) {
            view.setBackgroundResource(R.drawable.bg_list_even);
        } else if (position == 1) {
            view.setBackgroundResource(R.drawable.bg_list_odd);
        }
        return view;
    }
    

    holder class

    public class ViewHolder {
    
        public TextView title;
        public TextView description;
    }
    

    Make an array as given below as no of list item i suppose u have five items

     int[] color_arr={Color.BLUE,Color.CYAN,Color.DKGRAY,Color.GREEN,Color.RED};
    

    and after do in ur getView method of custome adapter as below

     public View getView(int position, View convertView, ViewGroup parent)
         {
    
         LayoutInflater inflater = getLayoutInflater();
         View row=convertView;
    
         row = inflater.inflate(R.layout.listview_custome, parent, false);
         row.setBackgroundColor(color_arr[position]);// this set background color
    
         TextView textview = (TextView) row.findViewById(R.id.tv_list);
         ImageView imageview = (ImageView) row.findViewById(R.id.iv_list);
    
         textview.setText(data_text[position]);
         imageview.setImageResource(data_image[position]);
    
         return (row);
    
        }
    
    public View getView(int position, View convertView, ViewGroup parent) {
    
        LayoutInflater inflater = getLayoutInflater();
        View rowView = convertView;
    
        rowView = inflater.inflate(R.layout.listview_custome, parent, false);
        rowView.setBackgroundColor(color_arr[position]);// this set background color
    
        TextView textview = (TextView) rowView.findViewById(R.id.tv_list);
        ImageView imageview = (ImageView) rowView.findViewById(R.id.iv_list);
    
        textview.setText(data_text[position]);
        imageview.setImageResource(data_image[position]);
        if (position == 0) {
            rowView.setBackgroundColor(Color.BLUE);
        }
        else if (position % 2 == 1) {
            rowView.setBackgroundColor(Color.RED);
        }
        else if (position % 2 == 0) {
            rowView.setBackgroundColor(Color.BLUE);
        }
        return (rowView);
    
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.