I'm trying to create an Android app with a grid of buttons in the main Activity. I need to be able to easily change the number of rows and columns of the grid so it has to be done programmatically through the Java and not in the XML.

I want the grid to take up the full height and width of its parent layout (currently a simple FrameLayout inside a ConstraintLayout) regardless of the button text. I wrote a method to generate the grid using a TableLayout and it almost works but the resulting grid doesn't take up the full height of the container. The colors are only there temporarily to facilitate troubleshooting as it makes it easy to see which parts are where. Below is the method I have currently:

 private void generateButtonTable(Context context, ViewGroup layout){

    int ROWS = 3;
    int COLUMNS = 3;

    TableLayout tableLayout = new TableLayout(context);
    tableLayout.setBackgroundColor(Color.RED); // This is taking up the full height and width of parent

    int button_id = 0;

    for (int i = 0; i < ROWS; i++){
        TableRow tableRow = new TableRow(context);

        tableRow.setLayoutParams(new TableRow.LayoutParams(
                0,
                0, // Set the height to 0, and the TableRow will use weight to occupy the remaining space
                1f // Set the weight to evenly distribute the height among rows
        ));

        tableRow.setBackgroundColor(Color.BLUE); // Only taking up full width

        for (int j = 0; j < COLUMNS; j++){
            Button button = new Button(context);

            button.setLayoutParams(new TableRow.LayoutParams(
                    0, // Set the width to 0, and the Button will use weight to occupy the remaining space
                    ViewGroup.LayoutParams.MATCH_PARENT, // Set the height to MATCH_PARENT
                    1f // Set the weight to evenly distribute the width
            ));

            button.setBackgroundColor(Color.GREEN);
            button.setText("button "+button_id++);
            tableRow.addView(button);
        }
        tableLayout.addView(tableRow, i);
    }
    layout.addView(tableLayout);
}

The table rows and buttons are taking up the full width correctly, but not the full height. For some reason setting the height of the rows to 0 and using the weight doesn't work.

Below is the onCreate method where it's called:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar); // Replaces the default ActionBar with our custom ToolBar

    FrameLayout layout = findViewById(R.id.grid_container);
    generateButtonTable(this, layout);

}

Here's the XML for the main Activity:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="0dp"
tools:context="com.therealsamchaney.beatcube.MainActivity">


<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar"
    />

<FrameLayout
    android:id="@+id/grid_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintBottom_toBottomOf="parent"
    android:background="@color/black"
    >

</FrameLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

How can I get the grid to take up the full height correctly? Do I need to switch to a GridLayout instead?

0

There are 0 best solutions below