BottomSheetDialog is a dialog that slides up from the bottom of the screen, partially overlaying the main content. It is commonly used for choices, menus, or actions that don't require a full-screen transition.
1. Creating a BottomSheetDialog
To instantiate a BottomSheetDialog, provide a context and a optional theme style. The style should set the dialog background to transparent so that a custom shape drawable can be applied.
BottomSheetDialog sheetDialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_bottom_sheet, null);
sheetDialog.setContentView(contentView);
// Optionally prevent dismissal on outside touch
// sheetDialog.setCanceledOnTouchOutside(false);
sheetDialog.show();
2. Layout Definition
Define the layout resource dialog_bottom_sheet.xml that will be inflated and set as the dialog content. The root container uses a custom background drawable for rounded corners.
<?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:background="@drawable/shape_bottom_sheet_rounded"
android:orientation="vertical">
<TextView
android:id="@+id/action_gallery"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="Choose from Gallery"
android:textColor="#191919"
android:textSize="15sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F5F5F5" />
<TextView
android:id="@+id/action_previous"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="View Previous Avatar"
android:textColor="#191919"
android:textSize="15sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F5F5F5" />
<TextView
android:id="@+id/action_save"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="Save to Device"
android:textColor="#191919"
android:textSize="15sp" />
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#F5F5F5" />
<TextView
android:id="@+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="Cancel"
android:textColor="#191919"
android:textSize="15sp" />
</LinearLayout>
3. Displaying the Dialog
Call sheetDialog.show() to present the bottom sheet. The dialog will animate from the bottom edge and overlay the current activity.
4. Adding Rounded Corners
Create a drawable resource shape_bottom_sheet_rounded.xml in the res/drawable directory. This shape applies rounded corners only to the top-left and top-right edges.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<solid android:color="@android:color/white" />
</shape>
To make the corners visible, the dialog's parent background must be transparent. Define the following styles in you're themes.xml (under res/values):
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
Then pass R.style.BottomSheetDialog as the second argument when constructing the BottomSheetDialog:
BottomSheetDialog sheetDialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);