Android Jetpack Compose – GridView using Firebase Firestore
GridView is also one of the most used UI components which is used to display items in the Grid format inside our app. By using this type of view we can display the items in the grid format. We have seen this type of GridView in most of the apps. We have also seen the implementation of GridView in our app. In this article, we will take a look at the implementation of GridView using Firebase Firestore in Android using Jetpack Compose.
What we are going to build in this article?
We will be building a simple application in which we will be displaying the data in the grid format and we will load this data from Firebase Firestore inside our GridView using Jetpack Compose. A sample video is given below to get an idea about what we are going to do in this article. Now we will move toward the implementation of this project.
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.
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.
Step 4: Connect your app to Firebase
After creating a new project. Navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot.
Inside that column Navigate to Firebase Cloud Firestore. Click on that option and you will get to see two options to Connect the app to Firebase and Add Cloud Firestore to your app. Click on Connect now option and your app will be connected to Firebase. After that click on the second option and now your App is connected to Firebase. After connecting your app to Firebase you will get to see the below screen.
After that verify that dependency for the Firebase Firestore database has been added to our Gradle file. Navigate to app > Gradle Scripts inside that file. Check whether the below dependency is added or not. If the below dependency is not present in your build.gradle file. Add the below dependency in the dependencies section.
After adding this dependency sync your project and now we are ready for creating our app. If you want to know more about connecting your app to Firebase. Refer to this article to get in detail about How to add Firebase to Android App.
Step 5: Working with AndroidManifest.xml
For adding data to Firebase we should have to give permissions for accessing the internet. for adding these permissions. navigate to app > AndroidManifest.xml. Inside that file add the below permissions to it.
Step 6: Working with MainActivity.kt file
Navigate to app>java>your app’s package name>MainActivity.kt file and add the below code to it. Comments are added in the code to get to know in detail.
Step 7: Adding the data to Firebase Firestore in Android
Search for Firebase in your browser and go to that website and you will get to see the below screen.
After clicking on the Go to Console option. Click on your project which is shown below.
After clicking on your project you will get to see the below screen. After clicking on this project you will get to see the below screen.
After clicking on Create Database option you will get to see the below screen.
Inside this screen, we have to select the Start in test mode option. We are using test mode because we are not setting authentication inside our app. So we are selecting Start in test mode. After selecting test mode click on the Next option and you will get to see the below screen.
Inside this screen, we just have to click on Enable button to enable our Firebase Firestore database. After completing this process we have to add the data inside our Firebase Console. For adding data to our Firebase Console. You have to click on Start Collection Option and give the collection name as Data. After creating a collection you have to click on the Auto option for creating the first document. Inside this create two fields as name and imgUrl and add values inside that fields. Add multiple documents in a similar way inside your Firebase Console. After that run your app and see the output of the app. After adding this data to Firebase run your app and see the output of the app.
Please Login to comment...