material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.28k stars 3.06k forks source link

[BottomSheet] Expanded bottom sheet with `fitToContents=false` is not full screen #3018

Open lwasyl opened 1 year ago

lwasyl commented 1 year ago

Description:

Simple bottom sheet that starts expanded and has fitToContents=false:

class MyBottomSheet : BottomSheetDialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
        inflater.inflate(R.layout.fragment_second, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        (dialog as BottomSheetDialog).behavior.apply {
            state = BottomSheetBehavior.STATE_EXPANDED
            isFitToContents = false
        }
    }
}

will appear flying on top of the screen:

https://user-images.githubusercontent.com/3951580/195555156-25e88b5d-082d-4b7d-a1b8-08e9122d9f8f.mov

Expected behavior: Bottom sheet is full-screen, laid out with match_parent height

Source code: above

Minimal sample app repro: bottom-sheet.zip

Material Library version: 1.6.1

afohrman commented 1 year ago

Hi @lwasyl, thanks for reporting this. Could you please attach a code sample or repo with any relevant code necessary to trigger this bug? It would be particularly helpful to see any XML layout files associated with the bottom sheet.

afohrman commented 1 year ago

Ah disregard the above comment, I didn't notice that you attached a sample app.

colorgold commented 1 year ago

I'm having the same problem. I've tried setting the design_bottom_sheet layout to MATCH_PARENT, but I get the same result as @lwasyl

@NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onShow: onCreateDialog");
        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

        dialog.setOnShowListener(dialog1 -> {
            ((BottomSheetDialog) dialog1).getBehavior().setFitToContents(false);
            ((BottomSheetDialog) dialog1).getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED);
            View bottomSheet = ((BottomSheetDialog) dialog1).findViewById(com.google.android.material.R.id.design_bottom_sheet);
            if (bottomSheet != null) {
                bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
            }
        });

        // Do something with your dialog like setContentView() or whatever
        return dialog;
    }
bbetter commented 1 year ago

any updates on this? have exactly same issue

afdanaj1117 commented 8 months ago

Also have same issue.. any solutions? or updates?

tbadalov commented 6 months ago

Having the same issue.

tbadalov commented 6 months ago

Not sure whether this is a bug or intended to be like this. Here is what helped me: https://stackoverflow.com/a/76283458/6657837

My onViewCreated method:

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View parentLayout = ((BottomSheetDialog) getDialog()).findViewById(com.google.android.material.R.id.design_bottom_sheet);
        ViewGroup.LayoutParams layoutParams = parentLayout.getLayoutParams();
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        parentLayout.setLayoutParams(layoutParams);
    }