Android GridView using Jetpack Compose
Last Updated :
17 Jun, 2022
GridView is the grid form of view in which the items within the grid are arranged in the grid layout. The data within the grid is to be added from the database or from the array list. In this article, we will be building a simple application for displaying the different programming languages within our grid view with their name using Jetpack Compose.
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. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Creating a modal class
Navigate to app > java > your app's package name > Right-click on it > New > Java/Kotlin class and name your class as GridModal and add the below code to it. Comments are added in the code to get to know in detail.
Kotlin
package com.example.newcanaryproject
// on below line we are creating a
// modal class for our data class.
data class GridModal(
// on below line we are creating two variable
// one is for language name which is string.
val languageName: String,
// next variable is for our
// image which is an integer.
val languageImg: Int
)
Step 3: Adding a new color in the Color.kt file
Navigate to app > java > your app's package name > ui.theme > Color.kt file and add the below code to it.
Kotlin
package com.example.newcanaryproject.ui.theme
import androidx.compose.ui.graphics.Color
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)
Step 4: Adding images to the drawable folder
Copy all the images which you want to add to your grid view. Navigate to app > res > drawable. Right-click on it and paste all the images into the drawable folder.
Step 5: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.example.newcanaryproject
import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.GridCells
import androidx.compose.foundation.lazy.LazyVerticalGrid
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.example.newcanaryproject.ui.theme.*
import java.util.*
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NewCanaryProjectTheme {
// on below line we are specifying
// background color for our application
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
// on below line we are specifying theme as scaffold.
Scaffold(
// in scaffold we are specifying top bar.
topBar = {
// inside top bar we are specifying background color.
TopAppBar(backgroundColor = greenColor,
// along with that we are specifying title for our top bar.
title = {
// in the top bar we are specifying tile as a text
Text(
// on below line we are specifying
// text to display in top app bar.
text = "Grid View Example",
// on below line we are specifying
// modifier to fill max width.
modifier = Modifier.fillMaxWidth(),
// on below line we are specifying text alignment.
textAlign = TextAlign.Center,
// on below line we are specifying color for our text.
color = Color.White
)
}
)
}
) {
// on below line we are calling grid
// view method to load our grid view.
gridView(LocalContext.current)
}
}
}
}
}
}
// on below line we are creating grid view function for loading our grid view.
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterialApi::class)
@Composable
fun gridView(context: Context) {
// on below line we are creating and initializing our array list
lateinit var courseList: List<GridModal>
courseList = ArrayList<GridModal>()
// on below line we are adding data to our list.
courseList = courseList + GridModal("Android", R.drawable.android)
courseList = courseList + GridModal("JavaScript", R.drawable.js)
courseList = courseList + GridModal("Python", R.drawable.python)
courseList = courseList + GridModal("C++", R.drawable.c)
courseList = courseList + GridModal("C#", R.drawable.csharp)
courseList = courseList + GridModal("Java", R.drawable.java)
courseList = courseList + GridModal("Node Js", R.drawable.nodejs)
// on below line we are adding lazy
// vertical grid for creating a grid view.
LazyVerticalGrid(
// on below line we are setting the
// column count for our grid view.
cells = GridCells.Fixed(2),
// on below line we are adding padding
// from all sides to our grid view.
modifier = Modifier.padding(10.dp)
) {
// on below line we are displaying our
// items upto the size of the list.
items(courseList.size) {
// on below line we are creating a
// card for each item of our grid view.
Card(
// inside our grid view on below line we are
// adding on click for each item of our grid view.
onClick = {
// inside on click we are displaying the toast message.
Toast.makeText(
context,
courseList[it].languageName + " selected..",
Toast.LENGTH_SHORT
).show()
},
// on below line we are adding padding from our all sides.
modifier = Modifier.padding(8.dp),
// on below line we are adding elevation for the card.
elevation = 6.dp
) {
// on below line we are creating a column on below sides.
Column(
// on below line we are adding padding
// padding for our column and filling the max size.
Modifier
.fillMaxSize()
.padding(5.dp),
// on below line we are adding
// horizontal alignment for our column
horizontalAlignment = Alignment.CenterHorizontally,
// on below line we are adding
// vertical arrangement for our column
verticalArrangement = Arrangement.Center
) {
// on below line we are creating image for our grid view item.
Image(
// on below line we are specifying the drawable image for our image.
painter = painterResource(id = courseList[it].languageImg),
// on below line we are specifying
// content description for our image
contentDescription = "Javascript",
// on below line we are setting height
// and width for our image.
modifier = Modifier
.height(60.dp)
.width(60.dp)
.padding(5.dp)
)
// on the below line we are adding a spacer.
Spacer(modifier = Modifier.height(9.dp))
// on below line we are creating
// a text for our grid view item
Text(
// inside the text on below line we are
// setting text as the language name
// from our modal class.
text = courseList[it].languageName,
// on below line we are adding padding
// for our text from all sides.
modifier = Modifier.padding(4.dp),
// on below line we are
// adding color for our text
color = Color.Black
)
}
}
}
}
}
Now run your application to see the output of it.
Output:
Similar Reads
ImageView in Android using Jetpack Compose
Images, graphics, and vectors attract many users as they provide a lot of information in a very informative way. ImageView in Android is used to display different types of images, from drawables to Bitmaps. In this article, we will take a look at the implementation of an ImageView in Android using J
3 min read
Staggered GridView in Android using Jetpack Compose
Staggered Grid View has been seen in most applications such as Pinterest in which each item of gridview takes its own height and aligns within the grid view according to that. In this article, we will look at how to implement Staggered Grid View in Android using Jetpack ComposeStep by Step Implement
4 min read
Text in Android using Jetpack Compose
Jetpack Compose is a new toolkit provided by Google. This is useful for designing beautiful UI designs. Android Text is a simple view in Android which is used to display text inside our App. In this article, we will take a look at the implementation of Text in Android using Jetpack Compose. Importan
5 min read
Color Gradient in Android using Jetpack Compose
In computer graphics, a color gradient specifies a range of position-dependent colors, usually used to fill a region. More than one color is displayed in a gradient where there is a color transition between any two colors. Most commonly, this transition could be vertical, horizontal, or radial. In t
3 min read
Button in Android using Jetpack Compose
Jetpack Compose is a new toolkit provided by Google. This is useful for designing beautiful UI designs. A Button is a UI component in Android which is used to navigate between different screens. With the help of a button, the user can interact with your app and perform multiple actions inside your a
3 min read
Curved Text in Android using Jetpack Compose
In Android, there are no inbuilt schemes to implement designed text like the word art, which is available in applications like MS Word. We can always refer to or download such schemes like designed font faces to implement designed text. However, it is also possible to natively build (building from s
3 min read
WebView in Android using Jetpack Compose
In Android, a WebView is an extension of View Class that allows us to display webpages. In simpler words, if you want to display a webpage in Activity Layout, then you can implement a WebView to display it. It is very similar to a browser but isn't one. A WebView can rather be referred to as a show
2 min read
Circular ImageView in Android using Jetpack Compose
Circular ImageView is used in many of the apps. These types of images are generally used to represent the profile picture of the user and many more images. We have seen the implementation of ImageView in Android using Jetpack Compose. In this article, we will take a look at the implementation of Cir
2 min read
Deep Links in Android using Jetpack Compose
A deep link is a URL that is used to direct users to a specific page or specific activity within the application. We can also pass data to our application with the help of these deep links. In this article, we will take a look at How to implement Deep Links in Android using Jetpack Compose. Step by
7 min read
Linkify in Android using Jetpack Compose
Linkify Class in Android Operating System is used to create user-clickable links from the Text on the basis of pattern matching and regex. In simple words, Linkify observes the text, finds out if a span or whole text is a type of pattern, and converts it into a clickable link. For example, if the te
3 min read