0% found this document useful (0 votes)
2 views

GridView

Uploaded by

tenor75805
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)
2 views

GridView

Uploaded by

tenor75805
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/ 3

Here's a simple Android Java program demonstrating the usage of GridView:

1. *Layout XML (activity_main.xml):*

xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp"

tools:context=".MainActivity">

<!-- GridView to display items -->

<GridView

android:id="@+id/gridView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:numColumns="3" />

</LinearLayout>

2. *Java Code (MainActivity.java):*

java

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Initialize GridView

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

// Create an ArrayList of items to display in the GridView

ArrayList<String> itemsList = new ArrayList<>(Arrays.asList(

"Item 1", "Item 2", "Item 3", "Item 4", "Item 5",

"Item 6", "Item 7", "Item 8", "Item 9", "Item 10"

));

// Create an ArrayAdapter to populate the GridView with items

ArrayAdapter<String> adapter = new ArrayAdapter<>(

this,

android.R.layout.simple_list_item_1,

itemsList

);

// Set the adapter for the GridView

gridView.setAdapter(adapter);

}
This program creates a simple layout with a GridView that displays a list of items. The GridView is
populated using an ArrayAdapter with a list of items. The layout XML specifies that the GridView should
display its items in 3 columns (android:numColumns="3").a

You might also like