Gautham Raj Ayyapparaj Gautham Raj Ayyapparaj - 7 days ago 5
Android Question

How to change Gridview's cell colour dynamically?

I am developing a simple android application for learning gridview. My requirement is to create a 2x2 grid which changes colours dynamically like a serial light circuit one by one, and others should be in white colour.

Please suggest a way to do this.

Answer

Create custom ArrayAdapter

new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, student_array) {
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);

    int color = 0x00FFFFFF; // Transparent
    if (condition) {
      color = 0x00000000; // Black
    }

    view.setBackgroundColor(color);

    return view;
  }
};
Comments