androidandroid-support-libraryandroid-support-design

How do I change the default height of a BottomSheetDialog?


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?


Solution

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