GridLayout
evenly stretched children
user interface design
layout management
Android development

GridLayout not GridView how to stretch all children evenly

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Making children stretch evenly in GridLayout is mostly about giving each child comparable row or column weight and letting the cell fill available space. Developers often expect GridLayout to behave like LinearLayout or GridView, but the mechanics are different. The reliable solution is to define the grid structure clearly and then make each child participate in that structure with equal weight or equivalent layout parameters.

Use Equal Column Weight for Even Width

When each child should take the same width across a row, give them equal column weight and let them fill horizontally.

xml
1<androidx.gridlayout.widget.GridLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:columnCount="2">
5
6    <Button
7        android:layout_width="0dp"
8        android:layout_height="wrap_content"
9        android:layout_columnWeight="1"
10        android:layout_gravity="fill_horizontal"
11        android:text="One" />
12
13    <Button
14        android:layout_width="0dp"
15        android:layout_height="wrap_content"
16        android:layout_columnWeight="1"
17        android:layout_gravity="fill_horizontal"
18        android:text="Two" />
19</androidx.gridlayout.widget.GridLayout>

The 0dp width tells the layout not to treat the view's measured width as final. The weight and fill behavior then determine how the available space is distributed.

Think in Terms of Cells, Not Absolute View Size

GridLayout places views into grid cells. If the grid definition is uneven, the children will look uneven even when their own parameters are similar.

That means you should first decide how many rows and columns the grid has, then decide whether each child occupies exactly one cell or spans multiple cells. Even stretching is easiest when the layout model itself is regular.

Programmatic Configuration Also Works

If you build the layout in code, set row and column specs with comparable weight values.

java
1GridLayout grid = new GridLayout(context);
2grid.setColumnCount(2);
3
4Button button = new Button(context);
5GridLayout.LayoutParams params = new GridLayout.LayoutParams();
6params.width = 0;
7params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
8params.setGravity(Gravity.FILL_HORIZONTAL);
9button.setLayoutParams(params);
10button.setText("One");
11grid.addView(button);

The main idea is the same as in XML: a weight-like column spec plus fill behavior.

Height Needs Its Own Rules

Stretching children evenly vertically is a separate question from stretching them evenly horizontally. If each row should have equal height, you need row constraints or consistent child layout behavior in that direction too.

Developers often fix one axis and then assume the other axis will follow automatically. GridLayout does not work that way. Each dimension needs to be described intentionally.

Avoid Fighting Intrinsic Measurement

Some widgets, especially text-heavy ones, strongly influence their own measured size. If a child insists on wrap-content behavior while others are weighted, the result may still look uneven.

That is why evenly stretched grid designs often work best when child content is compatible with the grid rather than trying to override it aggressively.

When GridLayout Is the Wrong Tool

If the requirement is a scrolling grid of repeated items, RecyclerView with a grid layout manager is usually a better choice. GridLayout is a layout container, not a data-backed repeating view like GridView or RecyclerView.

That distinction matters because some "stretch all children evenly" problems are really list-rendering problems in disguise.

Choosing the right container early saves a lot of layout work later.

Common Pitfalls

  • Expecting GridLayout to distribute size automatically without weights or explicit specs.
  • Forgetting to set 0dp width when using column weight in XML.
  • Fixing horizontal stretching and assuming vertical stretching will happen by itself.
  • Letting child wrap-content behavior fight the intended even grid appearance.
  • Using GridLayout for large scrolling datasets where RecyclerView would be more appropriate.

Summary

  • Even stretching in GridLayout depends on equal participation in the grid structure.
  • Use equal column or row weight plus fill behavior where appropriate.
  • Treat width and height as separate layout problems.
  • Keep the grid definition itself regular when you want regular output.
  • Use a data-backed grid view solution when the problem is really about repeated scrolling content.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.