GRID XML LAYOUT BY CODE

top banner

GRID XML LAYOUT BY CODE

In Android Studio, you can design a GRID XML LAYOUT BY CODE using XML code. Here is an example of how to create a GridLayout in Android Studio using XML code:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="3">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_column="0"
android:layout_row="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_column="1"
android:layout_row="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_column="0"
android:layout_row="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 4"
android:layout_column="1"
android:layout_row="1" />
</GridLayout>

This example creates a GridLayout with a width and height set to match the parent container. The columnCount and rowCount properties are set to 2 and 3 respectively, which creates a grid with 2 columns and 3 rows.
Inside the GridLayout, four Button elements are added. Each button has its layout_column and layout_row properties set, which determines the position of the button in the grid. In this example, button 1 is placed in column 0 and row 0, button 2 is placed in column 1 and row 0, button 3 is placed in column 0 and row 1, button 4 is placed in column 1 and row 1.

It is important to note that, you can add other views in place of button like ImageView, TextView, etc. And also you can adjust the layout_gravity, layout_rowSpan, layout_columnSpan properties to create more complex grid. Keep in mind that this code should be placed inside a layout file, such as activity_main.xml.
In conclusion, designing a GridLayout in Android Studio using XML code is a relatively simple process that involves adding a GridLayout element to your layout file and then adding child elements to the grid with their layout_column and layout_row properties set accordingly.

Leave a Reply

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