How to set the Maximum Height a Bottomsheet can go other wise WRAP_CONTENT?

Viewed 595

I have a bottomsheetDialogFragment having a nested scroll view having few texts as its child.

I have set the height of the parent layout to WRAP_CONTENT because I want to show the bottom sheet depends upon the height of the textView.

But if the Textview is large enough, it covers the whole screen. I want to show the bottom sheet up to a particular height and if the text view is small then it should follow WRAP_CONTENT. I have searched through the SO and not find a single answer to my query.

I could do another thing, I just measure the height of the text view before layout puts it and then decides the height dynamically. But I am amazed to see that there is no simple solution for this like the max height of the bottom sheet.

1 Answers

Set up a theme for it in styles.xml file like this :

<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>

<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
    <item name="behavior_peekHeight">500dp</item>
</style>

And then in your code file use it like this :

BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
            dialog.setContentView(R.layout.layout_bottom_sheet);
            dialog.show();
Related