how to change background color of grid in grid layout in android

how to change background color of grid in grid layout in android

To change the background color of a grid in a GridLayout in Android, you can use the setBackgroundColor method of the View class.

Here's an example of how you might do this in an activity:

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.GridLayout;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridLayout gridLayout = findViewById(R.id.grid_layout);
        int count = gridLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = gridLayout.getChildAt(i);
            view.setBackgroundColor(Color.RED);
        }
    }
}
Sourc‮al.www:e‬utturi.com

In this example, the setBackgroundColor method is used to set the background color of all the views in the GridLayout to red. The color is specified using the Color class.

You can use any valid color code to set the background color of the grid as desired.

Created Time:2017-11-01 12:05:13  Author:lautturi