1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.support.design.widget; 18 19 20 import android.os.Bundle; 21 import android.view.View; 22 import android.widget.Button; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 27 import com.example.android.support.design.R; 28 import com.google.android.material.bottomsheet.BottomSheetBehavior; 29 30 31 /** 32 * This demonstrates usage of {@link BottomSheetBehavior} with a FAB anchored to it. 33 */ 34 public class BottomSheetWithFab extends BottomSheetUsageBase { 35 36 private Button mToggle; 37 38 private View.OnClickListener mOnClickListener = new View.OnClickListener() { 39 @Override 40 public void onClick(View v) { 41 if (v.getId() == R.id.toggle && mBehavior != null) { 42 mToggle.setEnabled(false); 43 if (mBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) { 44 mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 45 } else { 46 mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); 47 } 48 } 49 } 50 }; 51 52 @Override getLayoutId()53 protected int getLayoutId() { 54 return R.layout.design_bottom_sheet_with_fab; 55 } 56 57 @Override onCreate(@ullable Bundle savedInstanceState)58 protected void onCreate(@Nullable Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 mToggle = findViewById(R.id.toggle); 61 mToggle.setOnClickListener(mOnClickListener); 62 mBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { 63 @Override 64 public void onStateChanged(@NonNull View bottomSheet, 65 @BottomSheetBehavior.State int newState) { 66 switch (newState) { 67 case BottomSheetBehavior.STATE_HIDDEN: 68 mToggle.setText(R.string.bottomsheet_show); 69 mToggle.setEnabled(true); 70 break; 71 case BottomSheetBehavior.STATE_EXPANDED: 72 case BottomSheetBehavior.STATE_COLLAPSED: 73 mToggle.setText(R.string.bottomsheet_hide); 74 mToggle.setEnabled(true); 75 break; 76 } 77 } 78 79 @Override 80 public void onSlide(@NonNull View bottomSheet, float slideOffset) { 81 } 82 }); 83 } 84 85 } 86