Android9/18/2020

How to Create a Modal Bottom Sheet in Android Using BottomSheetDialogFragment

Bottom Sheets are one of the most commonly used UI components in modern Android applications.

Applications use Bottom Sheets for:

  • Action menus
  • Filters
  • Payment options
  • Sharing dialogs
  • User interactions

In this tutorial, we will learn how to create a Modal Bottom Sheet in Android using BottomSheetDialogFragment.


What Is a Modal Bottom Sheet?

A Modal Bottom Sheet is a UI component that slides up from the bottom of the screen and temporarily blocks interaction with the underlying content.

It is commonly used to display:

  • Quick actions
  • Options menus
  • Confirmation dialogs
  • Interactive forms

What We Will Build

In this example:

  • User clicks a button
  • Modal Bottom Sheet opens
  • User selects an option
  • Bottom Sheet communicates back to the Activity

Step 1 — Add Material Dependency

Open your build.gradle file and add:


implementation 'com.google.android.material:material:1.11.0'

Then sync your Gradle project.


Step 2 — Create activity_main.xml

Create the main layout file.


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_open_bottom_sheet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Modal Bottom Sheet"/>

    <TextView
        android:id="@+id/text_view_button_clicked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Button X clicked"
        android:textSize="22sp"/>

</LinearLayout>

Step 3 — Create Bottom Sheet Layout

Create bottom_sheet_layout.xml.


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a Bottom Sheet"
        android:textSize="22sp"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"/>

</LinearLayout>

Step 4 — Create ExampleBottomSheetDialog.java

Create a new Java class named ExampleBottomSheetDialog.java.


package com.example.bottomsheetexample;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class ExampleBottomSheetDialog
        extends BottomSheetDialogFragment {

    private BottomSheetListener mListener;

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState
    ) {

        View view = inflater.inflate(
                R.layout.bottom_sheet_layout,
                container,
                false
        );

        Button button1 =
                view.findViewById(R.id.button1);

        Button button2 =
                view.findViewById(R.id.button2);

        button1.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        mListener.onButtonClicked(
                                "Button 1 clicked"
                        );

                        dismiss();
                    }
                });

        button2.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        mListener.onButtonClicked(
                                "Button 2 clicked"
                        );

                        dismiss();
                    }
                });

        return view;
    }

    public interface BottomSheetListener {

        void onButtonClicked(String text);
    }

    @Override
    public void onAttach(Context context) {

        super.onAttach(context);

        try {

            mListener =
                    (BottomSheetListener) context;

        } catch (ClassCastException e) {

            throw new ClassCastException(
                    context.toString()
                            + " must implement BottomSheetListener"
            );
        }
    }
}

Step 5 — Implement MainActivity.java

Open MainActivity.java and add the following code.


package com.example.bottomsheetexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
        implements ExampleBottomSheetDialog.BottomSheetListener {

    private TextView textViewResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        textViewResult =
                findViewById(
                        R.id.text_view_button_clicked
                );

        Button buttonOpenBottomSheet =
                findViewById(
                        R.id.button_open_bottom_sheet
                );

        buttonOpenBottomSheet.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        ExampleBottomSheetDialog bottomSheet =
                                new ExampleBottomSheetDialog();

                        bottomSheet.show(
                                getSupportFragmentManager(),
                                "exampleBottomSheet"
                        );
                    }
                });
    }

    @Override
    public void onButtonClicked(String text) {

        textViewResult.setText(text);
    }
}

How This Implementation Works

The workflow is:

  1. User clicks button
  2. Bottom Sheet appears from bottom
  3. User selects an option
  4. Bottom Sheet sends callback to Activity
  5. Activity updates UI

Why Interfaces Are Used

The Bottom Sheet communicates with the Activity using an interface.

This approach:

  • Reduces tight coupling
  • Improves modularity
  • Makes components reusable

Common Mistakes Developers Make

1. Forgetting Material Dependency

Without Material Components dependency, BottomSheetDialogFragment will not work properly.


2. Context Casting Errors

The Activity must implement the BottomSheetListener interface.


3. Using match_parent Height

Bottom Sheets should generally use wrap_content height for better UI behavior.


Modern Android Improvements

Modern Android applications can improve this implementation using:

  • Kotlin
  • Jetpack Compose
  • ViewBinding
  • MVVM architecture
  • Material 3 Bottom Sheets

Bottom Sheet vs Dialog

Bottom Sheet Dialog
Slides from bottom Appears in center
Modern mobile UX Traditional popup UI
Gesture-friendly Less interactive

FAQ

What is the difference between modal and persistent Bottom Sheets?

Modal Bottom Sheets block interaction temporarily, while persistent Bottom Sheets remain visible alongside app content.

Can Bottom Sheets contain RecyclerViews?

Yes. Bottom Sheets can contain complex layouts including RecyclerViews and forms.

Should Bottom Sheets be used instead of dialogs?

For modern Android mobile UX, Bottom Sheets are often preferred over traditional dialogs.


Conclusion

Bottom Sheets are an important part of modern Android UI design.

Using BottomSheetDialogFragment allows developers to create interactive and user-friendly modal components with clean communication patterns.

Modern Android applications should combine Material Design components with lifecycle-aware architecture for scalable UI development.


About the Author

Salil Jha is a Full Stack and Mobile Developer with experience in Android, React Native, scalable SaaS platforms, fintech systems, and developer tooling applications.

CodeChain Dev — Build Modern Products. Solve Real Problems.

Deep Structural Diagnostics.

Mastering JSON is only the first step. Use our industrial-grade workbench to format, validate, and synthesize models for your production APIs.