Last Updated: 30 Aug, 2022
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; the setAdapter() method is used to link the adapter with GridView. The main role of the adapter in GridView is to retrieve data from a database or array and place each piece of data in a suitable item to be shown in GridView. This project involves implementation using Java and Kotlin Programming Language for Android.
setAdapter()Java and Kotlin Programming Language for Android.
To initiate a new project in Android Studio, follow the steps outlined below:
By following these steps, you can set up a new project in Android Studio using Java or Kotlin programming languages.
To enhance your project, include the Google Repository in your settings.gradle file.
Ensure to synchronize the project after making these changes.
When working with XML files, particularly the activity_main.xml file representing the project's user interface:
Make necessary modifications in the activity_main.xml file to tailor the UI as needed.
It is pivotal to follow these steps diligently to ensure the successful execution of your Android Studio project.
XML Code Snippet for activity_main.xml |
---|
<?xml version="1.0" encoding="utf-8"?> |
Ensure that you create an XML layout file for each item displayed in the GridView. This can be achieved by following these steps:
Refer to the code snippet provided for the card_item.xml file for a better understanding of the structure.
androidx.cardview.widget.CardView
component.match_parent
and height to 120dp
.5dp
.app:cardCornerRadius
and app:cardElevation
respectively.100dp
and the source is set to a specific image resource.For instance, when creating a course display card, you can use the ImageView to show the course icon and the TextView to display the course name.
androidx.cardview.widget.CardView
.Step 5: Create a Model Class for Storing Data
When you are working on Android app development, specifically with GridView items, you need a Model Class to handle the data for each item. This Model Class, in Java or Kotlin, manages the information associated with every GridView item.
To create a Model Class named CourseModel, follow these steps:
Below is a snippet of the code for the CourseModel:
public class CourseModel { // String course_name for storing the course name // and imgid for storing the 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; } } |
Make sure to define the necessary attributes and methods within your CourseModel to effectively manage and retrieve data for your GridView items.
getCourse_name()
method returns the name of the course as a String.setCourse_name(course_name: String)
method allows setting the course name.getImgid()
and setImgid(imgid: Int)
handle the image ID.ArrayAdapter<CourseModel>
to adapt the data for display.getView()
method to customize how each item in the GridView is displayed.getView()
method inflates the layout for each item and binds data from the CourseModel to the views.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; |
getView()
method inflates the layout for each item if needed and binds data to the views.CourseGVAdapter
class is utilized for adapting the course data to be displayed in a GridView.ArrayAdapter
and is responsible for handling how the data is presented in the UI.CourseGVAdapter
class requires several key components to function effectively:android.content.Context
: This component provides information about the application's environment.android.view.LayoutInflater
: Used to inflate the layout of each item in the GridView.android.view.View
: Represents the basic building block for user interface elements.android.view.ViewGroup
: The parent view that holds the child views.getView
method is crucial in the CourseGVAdapter
class for rendering each item in the GridView.CourseModel
class holds information about a specific course, including its name and image ID.getCourse_name()
and getImgid()
are used to retrieve this data for display.courseTV
) and an image representing the course (courseIV
).CourseGVAdapter
, ensure that it properly inflates the layout for each item and sets the course name and image.convertView
and retrieve the correct CourseModel
for the current position.Course Name: Introduction to Android Development |
lateinit var coursesGV: GridView | Declares a GridView variable 'coursesGV' without initializing it immediately. |
override fun onCreate(savedInstanceState: Bundle?) | Overrides the onCreate method to initialize the activity when it is first created. |
setContentView(R.layout.activity_main) | Sets the activity content from a layout resource. |
coursesGV = findViewById(R.id.idGVcourses) | Finds the GridView with the specified id from the layout. |
courseModelArrayList.add(CourseModel("DSA", R.drawable.ic_gfglogo)) | Adds a CourseModel object for the "DSA" course with a specific drawable image. |
val adapter = CourseGVAdapter(this, courseModelArrayList) | Creates an adapter to populate the GridView with the course data. |
coursesGV.adapter = adapter | Sets the adapter for the GridView to display the course information. |
A GridView in Android is a view group that displays items in a two-dimensional scrolling grid. It is used to display items in a scrollable grid format.
It allows users to scroll through items horizontally and vertically, providing a flexible way to display a collection of items.
Developers can customize the appearance of items in the grid view by defining custom adapters and layouts for the grid cells.
For instance, a music app can use a GridView to display album covers in a grid layout, allowing users to scroll through and select albums.
|
Explore Courses for Software Development exam
|