0% found this document useful (0 votes)
5 views6 pages

Android GridView Tutorial with Code

The document provides a comprehensive guide on implementing a GridView in Android using Java and Kotlin, detailing the structure, XML attributes, and step-by-step instructions for creating a project. It covers essential components such as creating layout files, model classes, and adapter classes necessary for displaying data in a two-dimensional grid format. The document also includes example code for each part of the implementation, ensuring clarity for developers.

Uploaded by

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

Android GridView Tutorial with Code

The document provides a comprehensive guide on implementing a GridView in Android using Java and Kotlin, detailing the structure, XML attributes, and step-by-step instructions for creating a project. It covers essential components such as creating layout files, model classes, and adapter classes necessary for displaying data in a two-dimensional grid format. The document also includes example code for each part of the implementation, ensuring clarity for developers.

Uploaded by

brishpalsingh91
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GridView in Android with Example

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.

Some Important XML attributes of GridView

Attribute Description

android:numColumns Defines the number of columns to be set on the grid


android:horizontalSpacing Defines the spacing between two columns in the view
android:verticalSpacing Defines the spacing between two rows in the view

android:columnWidth Defines the width of each column. Use "auto_fit" for default
Attribute Description

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

Defines the type of selection mode to be used in the GridView. Possibles values are
android:choiceMode
"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 [Link] 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:

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


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">

<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="40dp"
android:numColumns="2"
android:verticalSpacing="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/card_item" />
</[Link]>

Design UI:

- 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:

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


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardUseCompatPadding="true"
app:cardCornerRadius="24dp"
app:cardElevation="5dp">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:orientation="vertical">

<ImageView
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher" />

<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/black"
android:textStyle="bold"
android:textAlignment="center" />
</LinearLayout>
</[Link]>

Design UI:

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:

Java
public class Model {
private String name;
private int image;

public Model(String name, int image) {


[Link] = name;
[Link] = image;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public int getImage() {


return image;
}

public void setImage(int image) {


[Link] = image;
}
}

Kotlin

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:

Java
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

public class GridViewAdapter extends ArrayAdapter<Model> {

public GridViewAdapter(Context context, ArrayList<Model> list) {


super(context, 0, list);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View itemView = convertView;


if (itemView == null) {
itemView = [Link](getContext()).inflate([Link].card_item, parent, false);
}

Model model = getItem(position);

TextView textView = [Link]([Link].text_view);


ImageView imageView = [Link]([Link].image_view);

if (model != null) {
[Link]([Link]());
[Link]([Link]());
}

return itemView;
}
}

Kotlin

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:

Java
package [Link];
import [Link];
import [Link];
import [Link];

import [Link];

public class MainActivity extends AppCompatActivity {

private GridView gridView;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

gridView = findViewById([Link].grid_view);
ArrayList<Model> list = new ArrayList<>();

[Link](new Model("DSA", [Link].gfg_logo));


[Link](new Model("JAVA", [Link].gfg_logo));
[Link](new Model("C++", [Link].gfg_logo));
[Link](new Model("Python", [Link].gfg_logo));
[Link](new Model("Javascript", [Link].gfg_logo));
[Link](new Model("DSA", [Link].gfg_logo));

GridViewAdapter adapter = new GridViewAdapter(this, list);


[Link](adapter);
}
}
Kotlin

Output:

You might also like