Android: Bottom Sheet
Bottom
sheet is a component that slides up from bottom of the screen to reveal
more content. You can find more detailed information of Bottom Sheet on
Google Material Design guidelines.
Adding resources
Add the latest appcompat and design support library to your project.
dependencies {
//replace X.X.X with the latest version
compile 'com.android.support:appcompat-v7:X.X.X'
compile 'com.android.support:design:X.X.X'
}
Make your activity extend AppCompatActivity.
public class ButtonActivity extends AppCompatActivity {
...
}
Creating layouts
Bottom sheet content
We’ll include the layouts for the sake of simplicity. This is the layout that will contain the content of the bottom sheet. File is bottom_sheet.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="340dp"
android:background="@android:color/darker_gray"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="80dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="@string/bottom_sheet_peek"
android:textColor="@android:color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/bottom_sheet_content"
android:textColor="@android:color/white" />
</LinearLayout>
behavior_peekHeight: Defines the height of the visible part.
behavior_hideable: Defines if the bottom sheet can be hidden by swiping it down.
Container view
Add the CoordinatorLayout as the root view. Then include the bottom sheet view as the direct child of the CoordinatorLayout. The layouts app_bar and activity_bottom_sheet_content are some view references that are not related to the bottom sheet. So you can replace or remove them.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.androidsample.BottomSheetActivity">
<!-- include app bar -->
<include layout="@layout/app_bar" />
<!-- include main content -->
<include layout="@layout/activity_bottom_sheet_content" />
<!-- include bottom sheet -->
<include layout="@layout/bottom_sheet" />
</android.support.design.widget.CoordinatorLayout>
At this point the bottom sheet dialog should work like this.
Dynamic control
Behaviors and attributes of the bottom sheet dialog can be controlled dynamically in Java as well.
// get the bottom sheet view
LinearLayout llBottomSheet = (LinearLayout) findViewById(R.id.bottom_sheet);
// init the bottom sheet behavior
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(llBottomSheet);
// change the state of the bottom sheet
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
// set the peek height
bottomSheetBehavior.setPeekHeight(340);
// set hideable or not
bottomSheetBehavior.setHideable(false);
// set callback for changes
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
That’s it.
Comments
Post a Comment