Bottom Sheets

MainActivity.java
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
 public class MainActivity extends AppCompatActivity {
  Button mButton1;
  @Override 
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   View bottomSheet = findViewById(R.id.design_bottom_sheet);
   final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
   behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override 
     public void onStateChanged(@NonNull View bottomSheet, int newState) {
    
    switch (newState) {
     
     case BottomSheetBehavior.STATE_DRAGGING:
       //Write your Logic here 
       Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_DRAGGING");
      
      break;
      
     case BottomSheetBehavior.STATE_SETTLING:
       //Write your Logic here 
       Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_SETTLING");
      
      break;
      
     case BottomSheetBehavior.STATE_EXPANDED:
       //Write your Logic here 
       Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_EXPANDED");
      
      break;
      
     case BottomSheetBehavior.STATE_COLLAPSED:
       //Write your Logic here 
       Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_COLLAPSED");
      
      break;
      
     case BottomSheetBehavior.STATE_HIDDEN:
       //Write your Logic here
       Log.i("BottomSheetCallback", "BottomSheetBehavior.STATE_HIDDEN");
      
      break;
      
    }
    
   }
    @Override 
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
     //Write your Logic here 
     Log.i("BottomSheetCallback", "slideOffset: " + slideOffset);
    
   }
   
  });
   Button button = (Button) findViewById(R.id.button);
   button.setOnClickListener(new View.OnClickListener() {
    @Override 
     public void onClick(View view) {
    
    if (behavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
      behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
     
    } else {
      behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
     
    }
   }
  });
 }
}


activity_main.xml
<?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=".MainActivity">
    <android.support.v4.widget.NestedScrollView
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
        <RelativeLayout 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" >
            <Button 
             android:id="@+id/button" 
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" 
             android:layout_centerHorizontal="true" 
             android:text="Show Bottom Sheet"/>  
        </RelativeLayout>  
    </android.support.v4.widget.NestedScrollView>  
    <RelativeLayout 
     android:id="@+id/design_bottom_sheet" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/colorAccent" 
     app:behavior_hideable="true" 
     app:behavior_peekHeight="50dp" 
     app:elevation="4dp" 
     app:layout_behavior="@string/bottom_sheet_behavior">  
        <TextView 
         android:id="@+id/bottomsheet_text"
         android:layout_width="wrap_content" 
         android:layout_height="match_parent"
         android:text="Introducing Bottom Sheets"
         android:textColor="#FFFFFF"/>
    </RelativeLayout> 
</android.support.design.widget.CoordinatorLayout> 

Share this