Android Jetpack Compose – Read Data From Firebase Firestore
In the previous article, we have seen How to add data to Firebase Firestore from our android application using Jetpack Compose. In this article, we will take a look at How to read this data from Firebase Firestore in our android application using Jetpack compose. We will be working on the previous application and adding functionality so that we can read the data which is added to our database. A sample video is given below to get an idea about what we are going to do in this article.
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: 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 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 3: Working with the AndroidManifest.xml file
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 4: Creating a modal class for the Course
Navigate to app>java>your app’s package name>Right click on it>New Kotlin class/file and name it as Course and add the below code to it. Comments are added to it to get to know it in detail.
Step 5: Create a new activity for reading the courses in our application
Navigate to app>java>your app’s package name> Right-click on it>New>Empty Compose Activity and name it as CourseDetailsActivity. After creating this activity add the below code to it. Comments are added in the code to get to know it in detail.
Step 6: 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.
After adding this code go to this link to open Firebase. After clicking on this link you will get to see the below page and on this page Click on Go to Console option in the top right corner.
After clicking on this screen you will get to see the below screen with your all project inside that select your project.
Inside that screen click n Firebase Firestore Database in the left window.
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 just have to run our application and add data inside our app and click on submit button. You will get to see the data added inside the Firebase Console.
Now run your application to see the output of it.
Please Login to comment...