How do I change the default height of a BottomSheetDialog?
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 BottomSheetBehavior
in 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
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, 2022Comments
-
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 theBottomSheetDialog
when I don't have direct access to theBottomSheetBehavior
? -
ianhanniballake about 8 yearsAt this point, you've replaced more of
BottomSheetDialog
than you are still using and should just roll your own right? -
Nikola Despotoski about 8 years@ianhanniballake My initial intention was to bypass
BottomSheetDialog
internal instance of theBottomSheetCallback
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 about 8 yearsNote there is a helper method
BottomSheetBehavior.from(View)
which could simplify your logic (although it throwsIllegalArgumentException
s) -
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 about 8 yearsThis does not work when using Theme.AppCompat.Light.NoActionBar as parent layout
-
Amit Jayant about 8 yearsI notice that behavior_peekHeight only takes effect if the value is greater than 208dp. Why is that?
-
ianhanniballake about 8 years@AmJay - that should be fixed in version 23.2.1. Make sure to update!
-
Amit Jayant about 8 yearsYes 23.2.1 fixed it. Thanks!!
-
Shirane85 about 8 yearsThis is not working for me, the bottom sheet takes the all screen's height.
-
Fang about 8 yearschange the behavior_peekHeight
-
Shirane85 about 8 yearsI did, obviously. but it doesn't matter what value i put.
-
Ajay Shrestha over 7 yearsawesome buddy. It saves my 8 hrs
-
Malwinder Singh about 7 yearsgetting this erro: Cannot resolve symbol 'Theme.Design.Light.BottomSheetDialog'
-
Malwinder Singh about 7 yearsgetting this erro: Cannot resolve symbol 'Theme.Design.Light.BottomSheetDialog'
-
Ray Li about 5 yearsSetting 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 about 5 yearsIt worked for me just changed to WRAP_CONTENT bottomSheet.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
-
Aguragorn about 4 yearsBottomSheetDialog.behavior is private now
-
Sindhu Arju almost 4 yearsgetParent() gives null
-
ivan8m8 almost 3 years@Aguragorn as I can see it's defined
public
-
Skizo-ozᴉʞS over 2 yearsThere's no getBehaviour() in BottomSheetDialogFragment