androidandroidxbottom-sheetandroid-bottomsheetdialogbottomsheetdialogfragment

Custom BottomSheet Behavior | More States | AndroidX


I want to be able to add more states to my current BottomSheet with multiple custom heights.

Right now, the AndroidX bottom sheet only has the following states: STATE_HIDDEN, STATE_HALF_EXPANDED, STATE_EXPANDED, STATE_COLLAPSED, STATE_DRAGGING, STATE_SETTLING.

Is there any way to add states/heights to the "BottomSheetBehavior" class?

Solutions I've considered:


Solution

  • There are 2 ways to achive that you want.

    First same solution like yours. There are a lot of examples of adding new states - like this. As you can see, they just copy class of BottomSheetBehavior and make some modification.

    Why it was done like that? Because most important fields that you need to modify are internal or private. There is no way to make modification by inheritance in case with private fields. You can check this anchor behavior and watch the difference with original in material repo. If all fields that you need to modify is internal you can make a child of bottom sheet behavior and place it in same package as bottom sheet behavior - check example here (this behavior is mine and i prefer this solutions, since all fields that i need is internal. check package of this class - it has same package name as BottomSheetBehavior). In this case you can modify fields. Both solutions are ugly. There is nothing we can do with this strange api of BottomSheetBehavior