Skip to content

Commit f09c562

Browse files
imhappipekingme
authored andcommitted
[Docs][BottomSheet] Add more information to BottomSheetDialogFragment docs
PiperOrigin-RevId: 686706770
1 parent 5441957 commit f09c562

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

catalog/java/io/material/catalog/bottomsheet/BottomSheetScrollableContentDemoFragment.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
import android.view.LayoutInflater;
2424
import android.view.View;
2525
import android.view.ViewGroup;
26+
import android.widget.FrameLayout;
2627
import androidx.annotation.LayoutRes;
2728
import androidx.annotation.NonNull;
2829
import androidx.annotation.Nullable;
2930
import androidx.core.view.WindowInsetsCompat;
30-
import com.google.android.material.bottomsheet.BottomSheetBehavior;
3131
import com.google.android.material.bottomsheet.BottomSheetDialog;
3232
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
3333
import com.google.android.material.internal.ViewUtils;
@@ -56,6 +56,7 @@ protected int getDemoContent() {
5656
/** A custom bottom sheet dialog fragment. */
5757
@SuppressWarnings("RestrictTo")
5858
public static class BottomSheet extends BottomSheetDialogFragment {
59+
5960
@NonNull
6061
@Override
6162
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
@@ -64,11 +65,13 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
6465
new BottomSheetDialog(
6566
getContext(), R.style.ThemeOverlay_Catalog_BottomSheetDialog_Scrollable);
6667
new WindowPreferencesManager(requireContext()).applyEdgeToEdgePreference(bottomSheetDialog.getWindow());
67-
bottomSheetDialog.setContentView(R.layout.cat_bottomsheet_scrollable_content);
68-
View bottomSheetInternal = bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
69-
BottomSheetBehavior.from(bottomSheetInternal).setPeekHeight(400);
68+
View content =
69+
LayoutInflater.from(getContext())
70+
.inflate(R.layout.cat_bottomsheet_scrollable_content, new FrameLayout(getContext()));
71+
bottomSheetDialog.setContentView(content);
72+
bottomSheetDialog.getBehavior().setPeekHeight(400);
7073

71-
View bottomSheetContent = bottomSheetInternal.findViewById(R.id.bottom_drawer_2);
74+
View bottomSheetContent = content.findViewById(R.id.bottom_drawer_2);
7275
ViewUtils.doOnApplyWindowInsets(bottomSheetContent, (v, insets, initialPadding) -> {
7376
// Add the inset in the inner NestedScrollView instead to make the edge-to-edge behavior
7477
// consistent - i.e., the extra padding will only show at the bottom of all content, i.e.,

docs/components/BottomSheet.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,33 @@ you need to use `Activity.getSupportFragmentManager()`.
405405
`BottomSheetDialogFragment`. You can override
406406
`onCancel(DialogInterface)` or `onDismiss(DialogInterface)` if necessary.
407407

408+
`BottomSheetDialogFragment` wraps the view in a `BottomSheetDialog`, which has
409+
its own `BottomSheetBehavior`. You can define your own `BottomSheetBehavior`
410+
through overriding `onCreateDialog`. Note that if overriding `onCreateDialog`,
411+
you should not override `onCreateView`.
412+
413+
```kt
414+
415+
import android.view.View
416+
import com.google.android.material.bottomsheet.BottomSheetBehavior
417+
418+
class ModalBottomSheet : BottomSheetDialogFragment() {
419+
420+
override fun onCreateDialog(
421+
savedInstanceState: Bundle?,
422+
): Dialog {
423+
val bottomSheetDialog: BottomSheetDialog =
424+
BottomSheetDialog(
425+
getContext(), R.style.ThemeOverlay_Catalog_BottomSheetDialog_Scrollable
426+
)
427+
bottomSheetDialog.setContentView(R.layout.bottom_sheet_content)
428+
// Set behavior attributes
429+
bottomSheetDialog.getBehavior().setPeekHeight(123)
430+
return bottomSheetDialog
431+
}
432+
}
433+
```
434+
408435
## Anatomy and key properties
409436

410437
Bottom sheets have a sheet, a drag handle, and, if modal, a scrim.

0 commit comments

Comments
 (0)