lostdev
lostdev

Reputation: 736

Android ListView set item background

I cannot seem to figure out how to make one or more listview item background change color.

I am currently trying to get the view through the adapter.

ViewGroup v = (ViewGroup) adapter.getView(i, null, listView);
for(int k = 0; k < v.getChildCount(); k++) {
    View child = v.getChildAt(k);
    child.setBackgroundColor(0xFFA6D2FF);
}
listView.invalidateViews();

I have tried setting the ViewGroup v that contains the text items (the list view items have sub items so it's 2 text views). I have also tried setting the child backgrounds, which may be working but it appears their bounds are 0's. So it may be working, but the children don't have a size even though you can see the text.

Upvotes: 0

Views: 1562

Answers (4)

Ghanshyam
Ghanshyam

Reputation: 25

as I understand. It's should be done by creating custom view in getView method of a custom adapter.

public class MyAdapter extends BaseAdapter {

     public Activity activity;
     ArrayList<String> data = new ArrayList<String>();
     private static LayoutInflater inflater = null;

     public MyAdapter(Activity a, ArrayList<String> d) {
      activity = a;
      data = d;
      inflater = LayoutInflater.from(activity);
      }    

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

        ViewHolder holder = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.my_list_item, null);
            holder = new ViewHolder();
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder)convertView.getTag();
        }

   convertView.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

           //Touch on view handle
         Log.d("", "Touched row "+position);
      }

   });

   //customizing view
    holder.textView = (TextView)convertView.findViewById(R.id.my_textview);
   holder.textView.setText(data.get(position));

   return convertView;
 }       

  public static class ViewHolder {
     public TextView textView;
  }
 @Override
  public int getCount() {
     return data.size();
  }
 @Override
 public Object getItem(int position) {
     return position;
 }
@Override
 public long getItemId(int position) {
     return position;
}

}

Background each row in listview

Upvotes: 0

Chirag Jain
Chirag Jain

Reputation: 1612

Better to change color of item from custom base adapter create following method:

private int[] colors;

// In Constructor or whenever you get data
colors = new int[sizeOfList];

private void setColors(int[] positions, int color) {
  for (int pos : positions) {
    colors[pos] = color;
   }
   notifyDataSetChanged();
}

private void setColors(int position, int color) {
   colors[pos] = color;
   notifyDataSetChanged();
}

public View getView(...) {

  if (colors[position] != 0) {

    child.setBackgroundColor(colors[position]);

  } else {

    child.setBackgroundColor(// default color);

  }

  return view;

}

Hope it helps. I think other answer also suggest you to do same. :)

Upvotes: 1

Apurv Gupta
Apurv Gupta

Reputation: 860

When you call listView.invalidateViews(); it will use getView() method of your adapter and draw the views. If you want to change the background color of a view in a ListView best place would be to do it in getView() of your adapter.

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    rowView.setBackgroundColor(0xFFA6D2FF);
    return rowView;
  }

Upvotes: 0

Blaze Tama
Blaze Tama

Reputation: 10948

If i understand your code correctly, you want to give a background color for each views you have in the list item.

If you want to do this programatically (not through XML), the easiest way that i can think is to create a custom adapter and set your holder (views) background color there.

Of course, you can do this very easily from XML.

See this for more information : http://www.vogella.com/tutorials/AndroidListView/article.html#tutorial_ownadapter

Feel free to comment if you have some questions or if i miss understood you.

Upvotes: 0

Related Questions