GRIDVIEW IN ANDROID

 

A GridView is a type of AdapterView that displays items in a two-dimensional scrolling grid.

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.

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.

 

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

<androidx.constraintlayout.widget.ConstraintLayout 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"

    tools:context=".MainActivity">

  

    <!-- android:numColumns=2 is the number of columns for Grid View

         android:horizontalSpacing is the space between horizontal grid items -->

    <GridView

        android:id="@+id/idGVcourses"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:horizontalSpacing="6dp"

        android:numColumns="2"

        android:verticalSpacing="6dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

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.

 

<?xml version="1.0" encoding="utf-8"?><!-- XML implementation of Card Layout -->

<androidx.cardview.widget.CardView

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="120dp"

    android:layout_gravity="center"

    android:layout_margin="5dp"

    app:cardCornerRadius="5dp"

    app:cardElevation="5dp">

  

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical">

  

        <ImageView

            android:id="@+id/idIVcourse"

            android:layout_width="100dp"

            android:layout_height="100dp"

            android:layout_gravity="center"

            android:src="@mipmap/ic_launcher" />

  

        <TextView

            android:id="@+id/idTVCourse"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="@string/app_name"

            android:textAlignment="center" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

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 > apps package name > Right-Click on it. Then Click on New > Java Class. Name your Java/Kotlin Class file as CourseModel. Below is the code for the <CourseModel file.

public class CourseModel {

  

    // string course_name for storing course_name 

    // and imgid for storing image id.

    private String course_name;

    private int imgid;

  

    public CourseModel(String course_name, int imgid) {

        this.course_name = course_name;

        this.imgid = imgid;

    }

  

    public String getCourse_name() {

        return course_name;

    }

  

    public void setCourse_name(String course_name) {

        this.course_name = course_name;

    }

  

    public int getImgid() {

        return imgid;

    }

  

    public void setImgid(int imgid) {

        this.imgid = imgid;

    }

}

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 > apps package name > Right-Click on it. Then Click on New > Java Class. Name your Java Class file as CourseGVAdapter. Below is the code for the CourseGVAdapter file.

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.TextView;

import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import java.util.ArrayList;

  

public class CourseGVAdapter extends ArrayAdapter<CourseModel> {

      

    public CourseGVAdapter(@NonNull Context context, ArrayList<CourseModel> courseModelArrayList) {

        super(context, 0, courseModelArrayList);

    }

  

    @NonNull

    @Override

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

        

        View listitemView = convertView;

        if (listitemView == null) {

            // Layout Inflater inflates each item to be displayed in GridView.

            listitemView = LayoutInflater.from(getContext()).inflate(R.layout.card_item, parent, false);

        }

        

        CourseModel courseModel = getItem(position);

        TextView courseTV = listitemView.findViewById(R.id.idTVCourse);

        ImageView courseIV = listitemView.findViewById(R.id.idIVcourse);

        

        courseTV.setText(courseModel.getCourse_name());

        courseIV.setImageResource(courseModel.getImgid());

        return listitemView;

    }

}

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.

import android.os.Bundle;

import android.widget.GridView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

  

public class MainActivity extends AppCompatActivity {

  

    GridView coursesGV;

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        coursesGV = findViewById(R.id.idGVcourses);

        ArrayList<CourseModel> courseModelArrayList = new ArrayList<CourseModel>();

        

        courseModelArrayList.add(new CourseModel("DSA", R.drawable.ic_gfglogo));

        courseModelArrayList.add(new CourseModel("JAVA", R.drawable.ic_gfglogo));

        courseModelArrayList.add(new CourseModel("C++", R.drawable.ic_gfglogo));

        courseModelArrayList.add(new CourseModel("Python", R.drawable.ic_gfglogo));

        courseModelArrayList.add(new CourseModel("Javascript", R.drawable.ic_gfglogo));

        courseModelArrayList.add(new CourseModel("DSA", R.drawable.ic_gfglogo));

  

        CourseGVAdapter adapter = new CourseGVAdapter(this, courseModelArrayList);

        coursesGV.setAdapter(adapter);

    }

}

 

Comments

Popular posts from this blog

SYSTEM CALLS ON OPERATING SYSTEM

TALLY ERP9