Search code examples
androidgridviewandroid-custom-view

How to change row colors in custom grid view android


What is have is custom grid view like this image : enter image description here

And as you can see i have change the background color for the first five cell to green , but the thing is the other rows i want the first one to be white and the second grey , the third white the forth grey and so on ..

Here is my custom adapter code :

public class CustomGridAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<String> itemsnew;
    LayoutInflater inflater;
    int value;

    public CustomGridAdapter(Context context, ArrayList<String> itemsNew) {
        this.context = context;
        this.itemsnew = itemsNew;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

        if (convertView == null) {

              convertView = inflater.inflate(R.layout.cell, null);


//        if  () {
//           convertView = inflater.inflate(R.layout.cell2, null);
//             EditText text2 = (EditText) convertView.findViewById(R.id.editText1);
//        
//         
//        }
//        else{
        }
        else if(position <5)
        {
            convertView = inflater.inflate(R.layout.cell, null);
            TextView text = (TextView) convertView.findViewById(R.id.grid_item);

           text.setTextColor(Color.parseColor("#703635"));
           text.setPadding(5, 0, 5, 0);
           LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
           //llp.setMargins(5, 5, 5, 5); // llp.setMargins(left, top, right, bottom);
           //llp.gravity=Gravity.RIGHT;

           text.setBackgroundResource(R.drawable.back3);
           text.setLayoutParams(llp);
          // text.setGravity(Gravity.RIGHT);
           text.setText(itemsnew.get(position));
        }
        else{
            convertView = inflater.inflate(R.layout.cell, null);
            TextView text = (TextView) convertView.findViewById(R.id.grid_item);

            text.setTextColor(Color.BLACK);
           text.setPadding(5, 0, 5, 0);
           LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
           //llp.setMargins(5, 5, 5, 5); // llp.setMargins(left, top, right, bottom);
           //llp.gravity=Gravity.RIGHT;

           text.setBackgroundResource(R.drawable.back2);
           text.setLayoutParams(llp);
          // text.setGravity(Gravity.RIGHT);
           text.setText(itemsnew.get(position));

        }
//        }
//        }
        return convertView;
    }

    @Override
    public int getCount() {
        return itemsnew.size();
    }

    @Override
    public Object getItem(int position) {
        return itemsnew.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

So how can i do this? any help? Note : odd and even wont work because the white row will contain 5,6,7,8,9 position , and the grey one will be 10,11,12,13,14 ..


Solution

  • You could do exactly what you doing except using a little bit more math logic such as:

    else if(position <5)
        {
            convertView = inflater.inflate(R.layout.cell, null);
            TextView text = (TextView) convertView.findViewById(R.id.grid_item);
    
           text.setTextColor(Color.parseColor("#703635"));
           text.setPadding(5, 0, 5, 0);
           LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
           //llp.setMargins(5, 5, 5, 5); // llp.setMargins(left, top, right, bottom);
           //llp.gravity=Gravity.RIGHT;
    
           text.setBackgroundResource(R.drawable.back3);
           text.setLayoutParams(llp);
          // text.setGravity(Gravity.RIGHT);
           text.setText(itemsnew.get(position));
        }
        else
         {
            convertView = inflater.inflate(R.layout.cell, null);
            TextView text = (TextView) convertView.findViewById(R.id.grid_item);
    
            text.setTextColor(Color.BLACK);
           text.setPadding(5, 0, 5, 0);
           LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
           //llp.setMargins(5, 5, 5, 5); // llp.setMargins(left, top, right, bottom);
           //llp.gravity=Gravity.RIGHT;
           if((position/5)%2 < 1){
              text.setBackgroundResource(R.drawable.back2); <<<<<<SET TO WHITE
           }else{
              text.setBackgroundResource(R.drawable.back4); <<<<<<SET TO GREY
           }
           text.setLayoutParams(llp);
          // text.setGravity(Gravity.RIGHT);
           text.setText(itemsnew.get(position));
        }
    

    Since your table is being set 5 positions at a time this will check by row. Also you will probably need to change the position variable into a float or long, not in the method but can do something like float pos = position*1.0f Try it and let me know.