How to Create Dynamic Bottom Sheet Dialog in Android using Firebase Firestore?
Bottom Sheet Dialog is one of the famous Material UI Component which is used to display the data or notifications in it. We can display any type of data or any UI component in our Bottom Sheet Dialog. In this article, we will take a look at the implementation of dynamic Bottom Sheet Dialog in Android using Firebase Firestore.
What we are going to build in this article?
We will be building a simple application in which we will be displaying a simple bottom sheet dialog, and we will display the data inside that bottom sheet dynamically from Firebase. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a new Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
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 on Connect 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 the app > AndroidManifest.xml. Inside that file add the below permissions to it.
Step 4: Working with the activity_main.xml file
As we are not displaying any UI inside our activity_main.xml. So we are not adding any UI component in our activity_main.xml because we are displaying the data in our custom layout file.
Step 5: Creating a new layout file for our Bottom Sheet Dialog
As we are displaying one image and text inside our bottom sheet dialog. So we will be building a custom layout for our Bottom Sheet Dialog. For creating a new layout file four our bottom sheet dialog box. Navigate to the app > res > layout > Right-click on it > Click on New > layout resource file and name it as bottom_sheet_layout and add below code to it.
Step 6: Adding a Bottom Sheet style to our styles.xml file
Navigate to the app > res > values > themes.xml file and add the below code to it in the resources file.
Step 7: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Step 8: Adding the data to Firebase Firestore Console in Android
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 on 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 data to Firebase Firestore in Android. For adding new data. Click on Start Collection Option, then specify collection Name as “BottomSheetDIalog” and after that click on Next, after that, specify Document ID Name as “Data“. Inside the Field section creates three fields as “textOne“, “textTwo“, and “Image” and pass values to it. After adding values to it and click on save and now run your application.