I'm using the BottomSheetBehavior
from Google recently released AppCompat v23.2. The height of my bottom sheet depends on the content displayed inside of the bottom sheet (similar to the what Google does themselves in their Maps app).
It works fine with the data loaded initially, but my application changes the content displayed during runtime and when this happens the bottom sheet retains at it's old height, which either leads to unused space at the bottom or a cut of view.
Is there any way to inform the bottom sheet layout to recalculate the height used for expanded state (when height of the ViewGroup
is set to MATCH_HEIGHT
) or any way to manually set the required height?
EDIT: I also tried to manually call invalidate()
on the ViewGroup
and the parent of it but without any success.
You can use
BottomSheetBehavior#setPeekHeight
for that.This does not automatically move the bottom sheet to the peek height. You can call
BottomSheetBehavior#setState
to adjust your bottom sheet to the new peek height.I had the same problem with
RelativeLayout
as my bottom sheet. The height won't be recalculated. I had to resort to setting the height by the new recalculated value and callBottomSheetBehavior.onLayoutChild
.This is my temporary solution:
I faced the same issue, when trying to update the peek height based on its contents, the height from a previous layout was found. This makes sense as the new layout had not taken place yet. By posting on the UI thread the layout height is calculated after the new layout, and another layout request is made to update the bottom sheet to the right height.
I've followed @HaraldUnander advice, and it gave me an idea which has actually worked. If you run a thread (couldn't make it work with the post method as him) after the
BottomSheetBehavior.state
is set up programmatically toSTATE_COLLAPSED
, then you can already obtain the height of your views and set thepeekHeight
depending on it's content.So first you set the
BottomSheetBehavior
:And then you set the peekHeight dynamically:
If using Java (I'm using Kotlin with Anko for threads), this could do:
Though the issue has been resolved in >=24.0.0 support library, if for some reason you still have to use the older version, here is a workaround.
For bottom sheet dialog fragment, read this: Bottom Sheet Dialog Fragment Expand Full Height