material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

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

Open lwasyl opened this issue 3 years ago • 7 comments
trafficstars

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

lwasyl avatar Oct 13 '22 09:10 lwasyl

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 avatar Feb 09 '23 20:02 afohrman

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

afohrman avatar Feb 09 '23 20:02 afohrman

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;
    }

colorgold avatar Aug 23 '23 21:08 colorgold

any updates on this? have exactly same issue

bbetter avatar Sep 04 '23 10:09 bbetter

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

afdanaj1117 avatar Jan 12 '24 15:01 afdanaj1117

Having the same issue.

tbadalov avatar Mar 09 '24 23:03 tbadalov

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);
    }

tbadalov avatar Mar 10 '24 11:03 tbadalov