Open In App

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

Last Updated : 06 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Create Dynamic Bottom Sheet Dialog in Android using Firebase Firestore Sample GIF

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.  

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
    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.  

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.  

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads