How do I change the default height of a BottomSheetDialog?

63,176

Solution 1

You can set a bottomSheetDialogTheme in your Activity, overriding the bottomSheetStyle attribute's behavior_peekHeight:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>

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

<style name="AppModalStyle"
       parent="Widget.Design.BottomSheet.Modal">
  <item name="behavior_peekHeight">@dimen/custom_peek_height</item>
</style>

This same technique can be used for other attributes as well, such as adding <item name="behavior_hideable">true</item> to the AppModalStyle to change whether the bottom sheet is hideable.

Solution 2

you can use BottomSheetBehaviorin your code

BottomSheetDialog dialog = new BottomSheetDialog(content);
.
.
.
dialog.setContentView(view);
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) view.getParent());
mBehavior.setPeekHeight(your dialog height)
dialog.show();

Solution 3

styles.xml

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

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

Solution 4

Woks for me

  override fun onStart() {
    super.onStart()
    dialog?.also {
        val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
        bottomSheet.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
        val behavior = BottomSheetBehavior.from<View>(bottomSheet)
        behavior.peekHeight = resources.displayMetrics.heightPixels //replace to whatever you want
        view?.requestLayout()
    }
}

Solution 5

Honestly, I've got no idea why nobody has mentioned these simple ways, yet:

override fun onResume() {
    super.onResume()

    dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
    // or since com.google.android.material:material:1.1.0-beta01
    (dialog as? BottomSheetDialog)?.behavior?.state = BottomSheetBehavior.STATE_EXPANDED

}
//or
dialog.behavior.peekheight = YOUR_VALUE

Directly answering the question

Q: How can I directly access the BottomSheetBehavior height?

A: dialog.behavior.peekheight

Share:
63,176
ianhanniballake
Author by

ianhanniballake

I work full time as an Android Framework Engineer at Google and I've also published three Android applications to Google Play which have over 150,000 installs over the past year.

Updated on July 21, 2022

Comments

  • ianhanniballake
    ianhanniballake almost 2 years

    I've been using the new BottomSheetDialog added in Support Library 23.2, but I want to change the default height of the dialog. I know it probably has to do with the behavior_peekHeight attribute which controls the initial height, but how do I set that in the BottomSheetDialog when I don't have direct access to the BottomSheetBehavior?

  • ianhanniballake
    ianhanniballake about 8 years
    At this point, you've replaced more of BottomSheetDialog than you are still using and should just roll your own right?
  • Nikola Despotoski
    Nikola Despotoski about 8 years
    @ianhanniballake My initial intention was to bypass BottomSheetDialog internal instance of the BottomSheetCallback which simply dismisses the dialog, and have my own for more control. Having instance of the behavior can be utilized for this scenario too.
  • ianhanniballake
    ianhanniballake about 8 years
    Note there is a helper method BottomSheetBehavior.from(View) which could simplify your logic (although it throws IllegalArgumentExceptions)
  • Nikola Despotoski
    Nikola Despotoski about 8 years
    @ianhanniballake In my case, you still need to point to the view that has the behavior otherwise, from() method doesn't walk the tree until it finds the behavior, as you said it will throw IAE. Yeah, it tidies up the code. Thanks.
  • DarkLeafyGreen
    DarkLeafyGreen about 8 years
    This does not work when using Theme.AppCompat.Light.NoActionBar as parent layout
  • Amit Jayant
    Amit Jayant about 8 years
    I notice that behavior_peekHeight only takes effect if the value is greater than 208dp. Why is that?
  • ianhanniballake
    ianhanniballake about 8 years
    @AmJay - that should be fixed in version 23.2.1. Make sure to update!
  • Amit Jayant
    Amit Jayant about 8 years
    Yes 23.2.1 fixed it. Thanks!!
  • Shirane85
    Shirane85 about 8 years
    This is not working for me, the bottom sheet takes the all screen's height.
  • Fang
    Fang about 8 years
    change the behavior_peekHeight
  • Shirane85
    Shirane85 about 8 years
    I did, obviously. but it doesn't matter what value i put.
  • Ajay Shrestha
    Ajay Shrestha over 7 years
    awesome buddy. It saves my 8 hrs
  • Malwinder Singh
    Malwinder Singh about 7 years
    getting this erro: Cannot resolve symbol 'Theme.Design.Light.BottomSheetDialog'
  • Malwinder Singh
    Malwinder Singh about 7 years
    getting this erro: Cannot resolve symbol 'Theme.Design.Light.BottomSheetDialog'
  • Ray Li
    Ray Li about 5 years
    Setting the peek height in styles does not work unless the Bottom Sheet Dialog is used inside a Coordinator Layout. The only way to set the peek height when using a BottomSheetDialogFragment is through setUpDialog(). See answer here: stackoverflow.com/a/38873329/6211703
  • kondal
    kondal about 5 years
    It worked for me just changed to WRAP_CONTENT bottomSheet.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
  • Aguragorn
    Aguragorn about 4 years
    BottomSheetDialog.behavior is private now
  • Sindhu Arju
    Sindhu Arju almost 4 years
    getParent() gives null
  • ivan8m8
    ivan8m8 almost 3 years
    @Aguragorn as I can see it's defined public
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 2 years
    There's no getBehaviour() in BottomSheetDialogFragment