The Wayback Machine - https://web.archive.org/web/20240930155358/https://www.geeksforgeeks.org/how-to-create-dynamic-bottom-sheet-dialog-in-android-using-firebase-firestore/
Open In App

How to Create Dynamic Bottom Sheet Dialog in Android using Firebase Firestore?

Last Updated : 12 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Bottom Sheet Dialog is one of the famous Material UI Component which is used to display 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 display 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.


firestore4-copy


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.  

firestore5-copy


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.  

implementation 'com.google.firebase:firebase-firestore:22.0.1'
implementation 'com.squareup.picasso:picasso:2.71828'

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.  

XML
<!--Permissions for internet-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


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.  

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/idRLBottomSheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:backgroundTint="@color/white"
    android:padding="20dp">

    <!--ImageView for displaying our image-->
    <ImageView
        android:id="@+id/idIVimage"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_margin="10dp"
        android:padding="5dp" />
    
    <!--Text view for displaying a heading text-->
    <TextView
        android:id="@+id/idTVtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_toRightOf="@id/idIVimage"
        android:padding="5dp"
        android:text="Message one"
        android:textColor="@color/black"
        android:textSize="15sp"
        android:textStyle="bold" />
    
    <!--Text View for displaying description text-->
    <TextView
        android:id="@+id/idTVtextTwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/idTVtext"
        android:layout_margin="10dp"
        android:layout_marginTop="10dp"
        android:layout_toEndOf="@id/idIVimage"
        android:padding="5dp"
        android:text="Message Two"
        android:textColor="@color/black"
        android:textSize="12sp" />

</RelativeLayout>


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. 

XML
<style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/BottomSheetStyle</item>
   </style>

   <style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@android:color/transparent</item>
</style>

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.

Java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

    // creating a variable for our firebase
    // firestore and relative layout of bottom sheet.
    FirebaseFirestore db;
    RelativeLayout bottomSheetRL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // initializing our variables.
        db = FirebaseFirestore.getInstance();
        bottomSheetRL = findViewById(R.id.idRLBottomSheet);
        
        // calling method to
        // display bottom sheet.
        displayBottomSheet();
    }

    private void displayBottomSheet() {
        
        // creating a variable for our bottom sheet dialog.
        final BottomSheetDialog bottomSheetTeachersDialog = new BottomSheetDialog(MainActivity.this, R.style.BottomSheetDialogTheme);
        
        // passing a layout file for our bottom sheet dialog.
        View layout = LayoutInflater.from(MainActivity.this).inflate(R.layout.bottom_sheet_layout, bottomSheetRL);
        
        // passing our layout file to our bottom sheet dialog.
        bottomSheetTeachersDialog.setContentView(layout);
        
        // below line is to set our bottom sheet dialog as cancelable.
        bottomSheetTeachersDialog.setCancelable(false);
        
        // below line is to set our bottom sheet cancelable.
        bottomSheetTeachersDialog.setCanceledOnTouchOutside(true);
        
        // below line is to display our bottom sheet dialog.
        bottomSheetTeachersDialog.show();
        
        // initializing our text views and image views.
        ImageView imageIV = layout.findViewById(R.id.idIVimage);
        TextView textOneTV = layout.findViewById(R.id.idTVtext);
        TextView textTwoTV = layout.findViewById(R.id.idTVtextTwo);
        
        // creating a variable for document reference.
        DocumentReference documentReference = db.collection("BottomSheetDIalog").document("Data");
        
        // adding snapshot listener to our document reference.
        documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
                // inside the on event method.
                if (error != null) {
                    // this method is called when error is not null 
                    // and we get any error
                    // in this case we are displaying an error message.
                    Toast.makeText(MainActivity.this, "Error found is " + error, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (value != null && value.exists()) {
                    // if the value from firestore is not null then we are
                    // getting our data and setting that data to our text view.
                    // after getting the value from firebase firestore
                    // we are setting it to our text view and image view.
                    textOneTV.setText(value.getData().get("textOne").toString());
                    Picasso.get().load(value.getData().get("Image").toString()).into(imageIV);
                    textTwoTV.setText(value.getData().get("textTwo").toString());
                }
            }
        });
    }
}

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.  

firestore1-copy


After clicking on Create Database option you will get to see the below screen.  

firestore2-copy


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.  


firestore3-copy



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.  

Output:



Similar Reads

How to Create Dynamic GridView in Android 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
9 min read
How to Create Dynamic Horizontal RecyclerView in Android using Firebase Firestore?
HorizontalRecyclerView is seen in many apps. It is generally used to display the categories in most apps and websites. This type of RecyclerView is seen in many E-commerce apps to indicate categories in the app. And this RecyclerView is also dynamic so that the admin can add or remove any item from that RecyclerView at any time. So in this article,
10 min read
How to Create Dynamic ListView in Android using Firebase Firestore?
ListView is one of the most used UI components in Android which you can find across various apps. So we have seen listview in many different apps. In the previous article, we have seen implementing ListView in Android using Firebase Realtime Database. Now in this article, we will take a look at the implementation of ListView using Firebase Firestor
9 min read
How to Create Dynamic Intro Slider in Android using Firebase Firestore?
We have seen creating a basic Intro Slider in Android which is used to inform our users regarding the features of our app and many more. In this article, we will take a look at the implementation of dynamic Intro Slider in our app with the help of Firebase. With the help of Firebase Firestore, we can change all the data dynamically from the Firebas
11 min read
How to Display Dynamic AlertDialog in Android using Firebase Firestore?
Dynamic AlertDialog is used in many different apps which are used to show different messages from dialog to the users. This type of dialog is also used for educating users with so many promotional banners. This type of alert dialog is generally dynamic and it displays images and texts which are dynamic in behavior and change after a certain interva
7 min read
How to Create and Add Data to Firebase Firestore in Android?
Firebase is a famous product of Google which is used by so many developers to add backend functionality for their website as well as apps. The Firebase will make your job really easier for backend database and handling the database. In this article, we will take a look at the implementation of Firebase Firestore in Android. This is a series of 4 ar
8 min read
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
10 min read
How to Setup Firebase Local Emulators for using Firebase Console and All Firebase Services?
Firebase Emulators are used to test our projects on the local firebase server (Emulator). It provides all the services of the Firebase Console. It is most useful for firebase functions because to use firebase functions we have to take the Blaze plan of firebase means we must have a credit card, here is a solution to this problem we can test our mob
4 min read
How to Read Data from Firebase Firestore in Android?
In the previous article, we have seen on How to Add Data to Firebase Firestore in Android. This is the continuation of this series. Now we will see How to Read this added data inside our Firebase Firestore. Now we will move towards the implementation of this reading data in Android Firebase. What we are going to build in this article? We will be cr
9 min read
How to Update Data in Firebase Firestore in Android?
In the previous article, we have seen on How to Add Data to Firebase Firestore in Android, How to Read the data from Firebase Firestore in Android. Now we will see How to Update this added data inside our Firebase Firestore. Now we will move towards the implementation of this updating data in Android Firebase. What we are going to build in this art
10 min read
Android Jetpack Compose - Update Data in Firebase Firestore
In the previous article, we have seen How to Add Data to Firebase Firestore in Android, How to read the data from Firebase Firestore in Android. Now we will see How to Update this added data inside our Firebase Firestore. Now we will move towards the implementation of this updating data in Android Firebase using Jetpack Compose. We will be creating
13 min read
Android Jetpack Compose - Delete Data in Firebase Firestore
In the previous article, we have taken a look at How to add, read and update data from our application in Firebase Firestore. In this article, we will take a look at How to implement a functionality in our application so that we will be able to delete data from Firebase Firestore in our android application using Jetpack Compose. A sample video is g
7 min read
How to Delete Data from Firebase Firestore in Android?
In the previous article, we have seen on How to Add Data, Read Data, and Update data in Firebase Firestore in Android. Now we will see How to Delete this added data inside our Firebase Firestore. So we will move towards the implementation of this deleting data in Android Firebase.   What we are going to build in this article?   We will be adding a
6 min read
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
15 min read
Android Jetpack Compose - Add Data to Firebase Firestore
Firebase is a famous backend service provider of Google which is used to create and manage databases that are required while creating an android application. In this article, we will take a look at How to use the Firebase Firestore database for adding our data to it. A sample video is given below to get an idea about what we are going to do in this
9 min read
How to Use Firebase Firestore as a Realtime Database in Android?
Firebase Firestore is the backend database that is used to add, read, update and delete data from Android. But in Firebase Firestore there is a separate method that is used to read data from Firebase Firestore in Realtime Database. In this article, we will read data from Firebase Firestore in Realtime Database. Note that we are going to implement t
6 min read
How to Fetch Data from Firebase Firestore in Batches with Limit in Android?
Firebase Firestore is a powerful NoSQL database provided by Firebase for Android app development. Fetching data from Firebase Firestore efficiently is crucial for Android developers aiming to create high-performance apps. Firestore provides various querying methods, including the ability to fetch data in batches with a limit, which is particularly
5 min read
Android Jetpack Compose - Create Dynamic WebView using Firebase Realtime Database
Converting a website into an application seems like a basic task to do on Android. With the help of WebView, we can show any webpage in our Android Application. We just have to implement the widget of WebView and add the URL inside the WebView that we have to load. So if you are looking for loading a website into your app which can be changed dynam
8 min read
How to Create Dynamic Horizontal RecyclerView in Android using Firebase Realtime Database?
HorizontalRecyclerView is seen in many apps. It is generally used to display the categories in most apps and websites. This type of RecyclerView is mostly seen in many E-commerce apps to indicate categories in the app. As we have already seen in Amazon Shopping App. So in this article, we will take a look at creating a Dynamic Horizontal Recycler V
7 min read
How to Create Dynamic WebView in Android with Firebase?
Converting a website into an application seems like a basic task to do on Android. With the help of WebView, we can show any webpage in our Android Application. We just have to implement the widget of WebView and add the URL inside the WebView which we have to load. So if you are looking for loading a website into your app which can be changed dyna
6 min read
How to Create Dynamic PDF Viewer in Android with Firebase?
If you are creating apps for students or for educational purposes then you need to add some PDF files for displaying some data inside our app. These PDF files are updated on regular basis. For loading this PDF from the server we prefer to use PDF Viewer which will load the PDF from the URL in Android. Inside this, we add the URL for PDF inside our
8 min read
How to Create a Dynamic Video Player in Android with Firebase Realtime Database?
Most of the apps use the video player to display so many videos inside their application. So for playing the video the app plays the video from its video URL. But what if we want to update that video on a real-time basis. So, in that case, we have to update our database and then later on we have to update our APK. So this is not an efficient way to
8 min read
How to Create a Dynamic Audio Player in Android with Firebase Realtime Database?
Many online music player apps require so many songs, audio files inside their apps. So to handle so many files we have to either use any type of database and manage all these files. Storing files inside your application will not be a better approach. So in this article, we will take a look at implementing a dynamic audio player in our Android app.
7 min read
How to Create Dynamic Auto Image Slider in Android with Firebase?
We have seen many apps that display images in the slider format as that of banners which slide automatically. This type of feature of the auto image slider is seen in many E-commerce sites. This feature is seen in many apps which are having images in them. In this article, we will take a look at How to Create a dynamic Auto Image Slider in Android.
9 min read
Sliding Bottom Sheet in Android using Jetpack Compose
Bottom Sheet is a material design component that slides up from the bottom of the screen to display additional content for the application. It is like a message that is invoked or appears upon user actions. A Bottom Sheet is widely used in modern applications. For example, a Bottom Sheet appears inside a game where there are in-app purchases to be
3 min read
How to Upload Excel Sheet Data to Firebase Realtime Database in Android?
Firebase Realtime Database is the backend service which is provided by Google for handling backend tasks for your Android apps, IOS apps as well as your websites. It provides so many services such as storage, database, and many more. The feature for which Firebase is famous for its Firebase Realtime Database. By using Firebase Realtime Database in
6 min read
Difference Between Modal and Persistent Bottom Sheet in Android
Android BottomSheet is a kind of view that is used as supplementary surfaces in mobile apps. This component is a part of the android design support library and is used to expose more data or information, menus, deep linked content, and in place of dialogs. It appears on the screen by sliding up from the bottom of the application screen and the user
3 min read
Modal Bottom Sheet in Android with Examples
In this article, we will learn about how to add Modal Bottom Sheet in our app. We have seen this UI component in daily applications like Google Drive, Maps, or Music Player App. The modal Bottom sheet always appears from the bottom of the screen and if the user clicks on the outside content then it is dismissed. It can be dragged vertically and can
3 min read
How to Create AlertDialog Box Using SweetAlert Dialog Library in Android?
In this article, we will learn about how to add Custom Alert Dialog in an app using the SweetAlert Dialog Library. It is a pop-up box that appears in response to any action of the user. AlertBox is very useful when it comes to validation, it can be used to display confirmation messages. Suppose the user presses the Back Button without saving the ch
3 min read
How to Create Progress Dialog Using ProgressBar and AlertDialog in Android?
In Android, a Progress bar is a UI element used to display the progress of any running task or an operation. An Alert Dialog is a type of alert message displayed over the screen that lets users choose between options to respond to the message of the alert. Both of these elements are different from each other in nature and in serving a purpose. Howe
3 min read