![]() |
VOOZH | about |
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.
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 |
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. |
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.
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.
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:
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.
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.
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.
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.
For learning more about the topic refer to these articles: