Links & Dependenciesactivity_main.xmlMainActivity.java
Design Support Library:
developer.android.com/topic/libraries/support-library/packages.html#design
<?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.codinginflow.persistentbottomsheetexample.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical"> <Button android:id="@+id/button_expand" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="expand" /> <Button android:id="@+id/button_collapse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="collapse" /> <TextView android:id="@+id/text_view_state" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="State" android:textColor="@android:color/black" android:textSize="30sp" /> </LinearLayout> <android.support.v4.widget.NestedScrollView android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="350dp" android:background="@android:color/holo_blue_light" app:behavior_hideable="true" app:behavior_peekHeight="55dp" app:layout_behavior="@string/bottom_sheet_behavior"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:text="This is a Bottom Sheet\n\nLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer" android:textSize="16sp" /> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout>
package com.codinginflow.persistentbottomsheetexample; import android.support.annotation.NonNull; import android.support.design.widget.BottomSheetBehavior; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private BottomSheetBehavior mBottomSheetBehavior; private TextView mTextViewState; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View bottomSheet = findViewById(R.id.bottom_sheet); mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); mTextViewState = findViewById(R.id.text_view_state); Button buttonExpand = findViewById(R.id.button_expand); Button buttonCollapse = findViewById(R.id.button_collapse); buttonExpand.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); } }); buttonCollapse.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); } }); mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { switch (newState) { case BottomSheetBehavior.STATE_COLLAPSED: mTextViewState.setText("Collapsed"); break; case BottomSheetBehavior.STATE_DRAGGING: mTextViewState.setText("Dragging..."); break; case BottomSheetBehavior.STATE_EXPANDED: mTextViewState.setText("Expanded"); break; case BottomSheetBehavior.STATE_HIDDEN: mTextViewState.setText("Hidden"); break; case BottomSheetBehavior.STATE_SETTLING: mTextViewState.setText("Settling..."); break; } } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { mTextViewState.setText("Sliding..."); } }); } }