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
17package com.example.android.support.design.widget;
18
19
20import android.os.Bundle;
21import android.support.annotation.NonNull;
22import android.support.annotation.Nullable;
23import android.support.design.widget.BottomSheetBehavior;
24import android.view.View;
25import android.widget.Button;
26
27import com.example.android.support.design.R;
28
29
30/**
31 * This demonstrates usage of {@link BottomSheetBehavior} with a FAB anchored to it.
32 */
33public class BottomSheetWithFab extends BottomSheetUsageBase {
34
35    private Button mToggle;
36
37    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
38        @Override
39        public void onClick(View v) {
40            if (v.getId() == R.id.toggle && mBehavior != null) {
41                mToggle.setEnabled(false);
42                if (mBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
43                    mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
44                } else {
45                    mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
46                }
47            }
48        }
49    };
50
51    @Override
52    protected int getLayoutId() {
53        return R.layout.design_bottom_sheet_with_fab;
54    }
55
56    @Override
57    protected void onCreate(@Nullable Bundle savedInstanceState) {
58        super.onCreate(savedInstanceState);
59        mToggle = findViewById(R.id.toggle);
60        mToggle.setOnClickListener(mOnClickListener);
61        mBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
62            @Override
63            public void onStateChanged(@NonNull View bottomSheet,
64                    @BottomSheetBehavior.State int newState) {
65                switch (newState) {
66                    case BottomSheetBehavior.STATE_HIDDEN:
67                        mToggle.setText(R.string.bottomsheet_show);
68                        mToggle.setEnabled(true);
69                        break;
70                    case BottomSheetBehavior.STATE_EXPANDED:
71                    case BottomSheetBehavior.STATE_COLLAPSED:
72                        mToggle.setText(R.string.bottomsheet_hide);
73                        mToggle.setEnabled(true);
74                        break;
75                }
76            }
77
78            @Override
79            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
80            }
81        });
82    }
83
84}
85