1

How set alternate cell Color in a grid ? I've found a lot of questions/tutorials about how to set row colors but nothing about cells' color. Thanks in advance

Whisher
  • 31,320
  • 32
  • 120
  • 201

2 Answers2

2

If you got a lot of examples for listView, why not just use getView method, as getView method is used for adapter and adapter is used in both views, list and grid. just set background of view according to the position of view in adapterview.

protected void getView(AdapterView<> adapterView, View convertView, int position, long id)
{

    LayoutInflater inflater = (LayoutInflater)context.getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);
    View view =inflater.inflate(yourlayout.xml, null);

    if(position%2==0)
        view.setBackgroundColor(color1);
    else
        view.setBackgroundColor(color2);
     return view;
}
jeet
  • 29,001
  • 6
  • 52
  • 53
  • Thanks for the tip, but I'm new at android so if you give me more hint. Steps 1) get the grid from within the my Activity GridView gridview = (GridView) findViewById(R.id.gridview); what's the step 2 – Whisher Jan 30 '12 at 11:14
  • I'm pleased if you show me the way step by step without the code. – Whisher Jan 30 '12 at 11:25
  • in your adapter of grid view override method getView – jeet Jan 30 '12 at 11:27
  • Sorry to bother you but it's just a little time I'm learning android stuff so where should I set up getView (in a Adpater or directly in the Activity) be patience :) – Whisher Jan 30 '12 at 19:52
  • If you can write your adapter code, I can edit your code to implement this. – jeet Jan 31 '12 at 04:24
  • Thanks jitendra for your patience. I should find all the logic around the android code.I don't know the class involved ..... I tried with – Whisher Jan 31 '12 at 08:03
  • import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; – Whisher Jan 31 '12 at 08:05
  • public class SpecialAdapter extends BaseAdapter { private Context mContext; private int[] colors = new int[] { 0x30FF0000, 0x300000FF }; public SpecialAdapter(Context c) { mContext = c; } public int getCount() { return 0; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { int colorPos = position % colors.length; convertView.setBackgroundColor(colors[colorPos]); return convertView; } } – Whisher Jan 31 '12 at 08:05
  • public class ListViewA extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new SpecialAdapter(this)); } } – Whisher Jan 31 '12 at 08:06
  • good point :) I don't see the grid. Do you mean that class does his work ? How can I see what's wrong I've not idea :( – Whisher Jan 31 '12 at 08:50
  • I've tried hard to edit the post but the forum code tag block me :( – Whisher Jan 31 '12 at 10:15
  • it's the same uffa ! http://www.sitepoint.com/forums/showthread.php?824910-Android-How-set-Alternate-Cell-Color-in-a-Grid&p=5050138#post5050138 – Whisher Jan 31 '12 at 10:20
2

A step forward and it works

public class ListViewA extends Activity {
GridView MyGrid;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MyGrid = (GridView) findViewById(R.id.gridview);
    MyGrid.setAdapter(new ImageAdapter(this));
}

public class ImageAdapter extends BaseAdapter {
    Context MyContext;

    public ImageAdapter(Context _MyContext) {
        MyContext = _MyContext;
    }

    @Override
    public int getCount() {
        return 9;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if (convertView == null) {

            LayoutInflater li = getLayoutInflater();
            view = li.inflate(R.layout.main, null);
        }

        if (position % 2 == 0)
            view.setBackgroundColor(0x30FF0000);
        else
            view.setBackgroundColor(0x300000FF);
        return view;

    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
}

}

Whisher
  • 31,320
  • 32
  • 120
  • 201