2

I have impelemented a BottomSheetDialog in my app but when I install it on a tablet and have the tablet laying down it does not expand fully on first click. It expands up to Collapsed state first and you have to drag it up to see everything. Why does it do this? Is it some setting you can change in your style?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    app:behavior_peekHeight="0dp"
    >
   ...

</LinearLayout>
val view = layoutInflater.inflate(R.layout.home_bottom_sheet_dialog, null)
val bottomSheetDialog = BottomSheetDialog(activity!!)

bottomSheetDialog.setContentView(view)
bottomSheetDialog.show()

I use API 22 AndroidX with kotlin.

enter image description here enter image description here

OscarCreator
  • 374
  • 4
  • 13
  • Check [this SO post](https://stackoverflow.com/questions/35937453/set-state-of-bottomsheetdialogfragment-to-expanded), I think it answers your question. – ljk Feb 16 '20 at 21:13

2 Answers2

5

As Sinan Ceylan said this part of the layout is not needed.

app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" app:behavior_peekHeight="0dp"

But to fix my problem I sat the peakHeight variable of BottomSheetBehavior to something large before it is shown.

bottomSheetDialog.setContentView(view)
bottomSheetDialog.behavior.peekHeight = 1000
bottomSheetDialog.show()
OscarCreator
  • 374
  • 4
  • 13
0

Implementing fully expanded bottom sheet a little tricky. You should override onViewCreated method in your BottomSheetDialogFragment class and listen GlobalLayout as below:

(Java Code)

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
        BottomSheetDialog dialog = (BottomSheetDialog) getDialog();

        if (dialog != null) {
            FrameLayout bottomSheet = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);
            if (bottomSheet != null) {

                BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                bottomSheetBehavior.setPeekHeight(0);

                bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                    @Override
                    public void onStateChanged(@NonNull View view1, int i) {
                        if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED || bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
                            if (!isStateSaved())
                                dismissAllowingStateLoss();
                        }
                    }

                    @Override
                    public void onSlide(@NonNull View view1, float v) {
                    }
                });
            }
        }
    });
}

Additionally no need that attributes in xml:

app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_peekHeight="0dp"
Sinan Ceylan
  • 1,043
  • 1
  • 10
  • 14