question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BottomSheetDialogFragment] What's the recommended way to customize the behavior of a BottomSheetDialogFragment?

See original GitHub issue

Let’s say I want my bottom sheet to skip its collapsed state. If I used a BottomSheetBehavior I could just set skipCollapse to true. What’s the recommended way to achieve that on a BottomSheetDialogFragment?

So far, I’ve been retrieving and modifying the BottomSheetDialogFragment’s inner BottomSheetBehavior myself, like so:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        dialog?.setOnShowListener { dialogInterface ->
            val sheetDialog = dialogInterface as? BottomSheetDialog

            val bottomSheet = sheetDialog?.findViewById<FrameLayout>(
                    com.google.android.material.R.id.design_bottom_sheet
            )

            bottomSheet?.let {
                val behavior = BottomSheetBehavior.from(it)
                behavior.skipCollapsed = true
                // any other behavior modification here
            }
        }
    }

However, this approach seems kind of hackish to me. Is there any alternative or is this the recommended way to achieve what I want?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
iRYO400commented, Apr 17, 2020

I’ve found a convenient way to config bottom sheet behavior using styles.

  1. Customize default BottomSheet by overriding it’s attributes, for example in styles.xml
    <style name="Widget.MyApp.BottomSheet.Modal" parent="Widget.MaterialComponents.BottomSheet.Modal">
        <item name="behavior_skipCollapsed">true</item>
        <item name="behavior_fitToContents">true</item>
        <item name="behavior_peekHeight">1000dp</item> // yep, that helped to skip collapsed state at initial
        <item name="behavior_hideable">true</item>
    </style>

Go inside Widget.MaterialComponents.BottomSheet.Modal to see what settings you can modify.

  1. Then create custom theme inherited from Theme.Design.BottomSheetDialog and set that you want to override bottom sheet’s style with you own. Also can be placed in styles.xml
    <style name="Theme.MyApp.BottomSheetDialog" parent="Theme.Design.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/Widget.MyApp.BottomSheet.Modal</item>
    </style>

  1. And the last, define your new created theme for bottom sheet dialog in your Activity’s theme or Application’s theme, which placed in themes.xml(hope you follow Google’s recommendations about packaging styles&themes)
<style name="Base.Theme.MyApp" parent="Base.Theme.Root">
    ... too many other things
    <item name="bottomSheetDialogTheme">@style/Theme.MyApp.BottomSheetDialog</item>
</style>
5reactions
xanscalecommented, Jun 10, 2020

@renanferrari inside onViewCreated i used that

val behavior: BottomSheetBehavior<*> = (dialog as BottomSheetDialog).behavior
behavior.halfExpandedRatio = 0.3f
behavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED

i don’t know if it is the correct way

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - Set state of BottomSheetDialogFragment to expanded
The Behavior is hidden in BottomSheetDialog, which is available to get the behavior If you would like not to change your parent layout...
Read more >
BottomSheetDialogFragment Made Simpler | by Guneet Singh
If this is the first time you are using BottomSheet in your android app then this the best place to get started. The...
Read more >
Full height bottom sheet dialog fragment on start ... - YouTube
if you need to make a bottom sheet dialog fragment take the whole screen height on start watch the video to see how.!This...
Read more >
Android Bottom Sheet Behavior and Animated Button on Top ...
BottomSheetDialogFragment is a thin layer on top of the regular support library Fragment that renders your fragment as a modal bottom sheet.
Read more >
BottomSheetDialogFragment - Android Developers
DialogFragment. ↳, androidx.appcompat.app.AppCompatDialogFragment. ↳, com.google.android.material.bottomsheet.BottomSheetDialogFragment ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found