Skip to content

Commit

Permalink
[Docs][BottomSheet] Add more information to BottomSheetDialogFragment…
Browse files Browse the repository at this point in the history
… docs

PiperOrigin-RevId: 686706770
  • Loading branch information
imhappi authored and pekingme committed Oct 17, 2024
1 parent 5441957 commit f09c562
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.WindowInsetsCompat;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.internal.ViewUtils;
Expand Down Expand Up @@ -56,6 +56,7 @@ protected int getDemoContent() {
/** A custom bottom sheet dialog fragment. */
@SuppressWarnings("RestrictTo")
public static class BottomSheet extends BottomSheetDialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Expand All @@ -64,11 +65,13 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
new BottomSheetDialog(
getContext(), R.style.ThemeOverlay_Catalog_BottomSheetDialog_Scrollable);
new WindowPreferencesManager(requireContext()).applyEdgeToEdgePreference(bottomSheetDialog.getWindow());
bottomSheetDialog.setContentView(R.layout.cat_bottomsheet_scrollable_content);
View bottomSheetInternal = bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheetInternal).setPeekHeight(400);
View content =
LayoutInflater.from(getContext())
.inflate(R.layout.cat_bottomsheet_scrollable_content, new FrameLayout(getContext()));
bottomSheetDialog.setContentView(content);
bottomSheetDialog.getBehavior().setPeekHeight(400);

View bottomSheetContent = bottomSheetInternal.findViewById(R.id.bottom_drawer_2);
View bottomSheetContent = content.findViewById(R.id.bottom_drawer_2);
ViewUtils.doOnApplyWindowInsets(bottomSheetContent, (v, insets, initialPadding) -> {
// Add the inset in the inner NestedScrollView instead to make the edge-to-edge behavior
// consistent - i.e., the extra padding will only show at the bottom of all content, i.e.,
Expand Down
27 changes: 27 additions & 0 deletions docs/components/BottomSheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,33 @@ you need to use `Activity.getSupportFragmentManager()`.
`BottomSheetDialogFragment`. You can override
`onCancel(DialogInterface)` or `onDismiss(DialogInterface)` if necessary.

`BottomSheetDialogFragment` wraps the view in a `BottomSheetDialog`, which has
its own `BottomSheetBehavior`. You can define your own `BottomSheetBehavior`
through overriding `onCreateDialog`. Note that if overriding `onCreateDialog`,
you should not override `onCreateView`.

```kt

import android.view.View
import com.google.android.material.bottomsheet.BottomSheetBehavior

class ModalBottomSheet : BottomSheetDialogFragment() {

override fun onCreateDialog(
savedInstanceState: Bundle?,
): Dialog {
val bottomSheetDialog: BottomSheetDialog =
BottomSheetDialog(
getContext(), R.style.ThemeOverlay_Catalog_BottomSheetDialog_Scrollable
)
bottomSheetDialog.setContentView(R.layout.bottom_sheet_content)
// Set behavior attributes
bottomSheetDialog.getBehavior().setPeekHeight(123)
return bottomSheetDialog
}
}
```

## Anatomy and key properties

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

0 comments on commit f09c562

Please sign in to comment.