I'm trying to use the support library's BottomSheetDialogFragment to replicate the standard sheet that shows when you tap a Share button (see below). How would I achieve a similar layout, where there's a title at the top, independently scrollable content in the center, but an bottom anchored view with buttons that always stay on top.
Asked
Active
Viewed 3,587 times
2 Answers
0
You need to build a custom layout for the bottomSheet, for example share_bottom.xml. Within that layout you could
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_hideable="true"
app:behavior_peekHeight="200dp">
<TextView
android:layout_width="match_parent"
android:text="header"
android:layout_height="wrap_content"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
And then include it at he bottom of your fragment's layout:
After that you can control the visibility of this sheet
//retrieve the bottomsheet
bottomSheet = (LinearLayout) findViewById(R.id.bottomSheet);
//get the behaviour controller
bsb = BottomSheetBehavior.from(bottomSheet);
//hide the sheet
bsb.setState(BottomSheetBehavior.STATE_HIDDEN);
//showthe sheet
bsb.setState(BottomSheetBehavior.STATE_EXPANDED);

Guille89
- 81
- 8
-
Not quite, since I need it to be a Modal Sheet just like google's Share sheet. The modal sheet comes up nicely, and slowly, while dimming the rest of the screen. The bottom two buttons stay pinned below the screen while the nested scrollview in the center scrolls. – strangetimes Jun 08 '16 at 20:51
0
I was also having the same problem. I solved using these steps.
- Make FrameLayout as your root layout.
- Include the view to be anchored as second child and first child should contain all the content of activity.
- Animate the second child to be visible when bottom sheet is expanded and make it invisible when bottom sheet is collapsed.
- The independently scrolling content inside bottom sheet can be acheived by using a scroll view or nested scroll view inside bottom sheet.
- To dim background refer this link
This is just a workaround. Basically what I did is replicate the persistent bottom sheet to behave like modal sheet.
-
1Thanks - yes that does sound a lot like a hack. I'm looking for a clean, future proof solution. It seems there probably isn't a way to do this without overriding the onlayout method or something then. – strangetimes Jun 17 '16 at 19:19