GridLayout in Android

A GridLayout is similar to a TableLayout, but the number of columns must be specified using a columnCount attribute.

GridLayout example

<GridLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:columnCount=“3”>

<!— 1st row, spanning 3 columns —>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“Enter your name”

android:layout_columnSpan=“3”

android:textSize=“26sp”

/>

<!— 2nd row —>

<TextView android:text=“First Name”/>

<EditText

android:id=”@+id/firstName”

android:layout_width=“200dp”

android:layout_columnSpan=“2”/>

<!— 3rd row —>

<TextView android:text=“Last Name”/>

<EditText

android:id=”@+id/lastName”

android:layout_width=“200dp”

android:layout_columnSpan=“2”/>

<!— 4th row, spanning 3 columns —>

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_column=“2”

android:layout_gravity=“right”

android:text=“Submit”/>

</GridLayout>

Leave a Reply

Your email address will not be published. Required fields are marked *