0% found this document useful (0 votes)
17 views2 pages

Mad Learnng Grid View

The document provides instructions for creating a simple Android application with a GridView layout. It includes an XML layout file (activity_main.xml) defining a vertical LinearLayout containing a GridView, and a Java code file (MainActivity.java) that populates the GridView with numbers from one to nine. When the app is run, it displays a 3x3 grid of these numbers.

Uploaded by

xyz935613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Mad Learnng Grid View

The document provides instructions for creating a simple Android application with a GridView layout. It includes an XML layout file (activity_main.xml) defining a vertical LinearLayout containing a GridView, and a Java code file (MainActivity.java) that populates the GridView with numbers from one to nine. When the app is run, it displays a 3x3 grid of these numbers.

Uploaded by

xyz935613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Create activity_main.

xml (Layout File)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:padding="10dp"/>
</LinearLayout>

Create MainActivity.java (Java Code)


package com.example.gridviewexample;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

GridView gridView = findViewById(R.id.gridView);

// Array of data to display in GridView


String[] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};

// Adapter to bind data to GridView


ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, numbers);

// Setting adapter to GridView


gridView.setAdapter(adapter);
}
}

Expected Output

When you run the app, a 3x3 GridView will display numbers like this:
CopyEdit
1 2 3
4 5 6
7 8 9

You might also like