VOOZH about

URL: https://www.geeksforgeeks.org/android/gridview-in-android-with-example/

⇱ GridView in Android with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

GridView in Android with Example

Last Updated : 23 Jul, 2025

A GridView is a type of AdapterView that displays items in a two-dimensional scrolling grid. Items are inserted into this grid layout from a database or from an array. The adapter is used for displaying this data, setAdapter() method is used to join the adapter with GridView. The main function of the adapter in GridView is to fetch data from a database or array and insert each piece of data in an appropriate item that will be displayed in GridView. This is what the GridView structure looks like. We are going to implement this project using both Java and Kotlin Programming Language for Android.

👁 GridView Orientation

Some Important XML attributes of GridView

Attribute

Description

android:numColumnsDefines the number of columns to be set on the grid
android:horizontalSpacingDefines the spacing between two columns in the view
android:verticalSpacingDefines the spacing between two rows in the view

android:columnWidth

Defines the width of each column. Use "auto_fit" for default

android:stretchMode

Defines the stretch capability of column to fill the grid's available width. Possible values are "none", "spacingWidth", "columnWidth", and "spacingWidthUniform"

android:choiceMode

Defines the type of selection mode to be used in the GridView. Possibles values are "none", "singleChoice", "multipleChoice", and "multipleChoiceModal"

android:scrollbars

Defines the type of scrollbars for the GridView.


Step By Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio .

The code for that has been given in both Java and Kotlin Programming Language for Android.


Step 2: Add the Required Dependencies

Add the Google Repository to your settings.gradle.kts File.

repositories {
 // add the following
 google()
 mavenCentral()
}

After adding this Dependency, sync the Project and now we will move towards its implementation.


Step 3: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

activity_main.xml:

Design UI:

👁 Layout_1


- Create an XML layout file for each item of GridView

Create an XML file for each grid item to be displayed in GridView. Click on the app > res > layout > Right-Click > Layout Resource file and then name the file as card_item . Below is the code for the card_item.xml file.

card_item.xml:

Design UI:

👁 Layout_2


Step 5: Create a Model Class for Storing Data

Model Class is the Java/Kotlin Class that handles data to be added to each GridView item of GridView. For Creating Model Class. Now click on app > java/kotlin > apps package name > Right-Click on it. Then Click on New > Java Class/Kotlin Class. Name your Java/Kotlin Class file as CourseModel. Below is the code for the < CourseModel file.

CourseModel File:


Step 6: Create an Adapter Class

Adapter Class adds the data from Modal Class in each item of GridView which is to be displayed on the screen. For Creating Adapter Class. Now click on app > java/kotlin > {package-name-directory} > Right-Click on it. Then Click on New > Java/Kotlin Class. Name your Java/Kotlin Class file as GridViewAdapter. Below is the code for the GridViewAdapter file.

GridViewAdapter File:

Step 7: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

MainActivity File:

Output:

👁 Output GrindView

For learning more about the topic refer to these articles:

Comment

Explore