Skip to content

Commit b701cea

Browse files
Material Design Teammelaniegoetz
authored andcommitted
Extended FAB demo.
PiperOrigin-RevId: 231689691
1 parent 63d11da commit b701cea

8 files changed

Lines changed: 408 additions & 5 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2018 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+
* https://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 io.material.catalog.fab;
18+
19+
import io.material.catalog.R;
20+
21+
import android.os.Bundle;
22+
import androidx.annotation.LayoutRes;
23+
import androidx.annotation.Nullable;
24+
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
25+
import com.google.android.material.snackbar.BaseTransientBottomBar;
26+
import com.google.android.material.snackbar.Snackbar;
27+
import androidx.appcompat.app.AppCompatActivity;
28+
import androidx.appcompat.widget.Toolbar;
29+
import android.view.LayoutInflater;
30+
import android.view.View;
31+
import android.view.ViewGroup;
32+
import io.material.catalog.feature.DemoFragment;
33+
import io.material.catalog.feature.DemoUtils;
34+
import java.util.List;
35+
36+
/**
37+
* An Extended FAB motion demo fragment which demonstrate the FAB behavior when positioned over a
38+
* scrollable component.
39+
*/
40+
public class ExtendedFabBehaviorDemoFragment extends DemoFragment {
41+
42+
@Nullable
43+
@Override
44+
public View onCreateDemoView(
45+
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
46+
View root =
47+
layoutInflater.inflate(getExtendedFabContent(), viewGroup, false /* attachToRoot */);
48+
49+
Toolbar toolbar = root.findViewById(R.id.toolbar);
50+
AppCompatActivity activity = (AppCompatActivity) getActivity();
51+
activity.setSupportActionBar(toolbar);
52+
53+
List<ExtendedFloatingActionButton> extendedFabs =
54+
DemoUtils.findViewsWithType(root, ExtendedFloatingActionButton.class);
55+
for (ExtendedFloatingActionButton extendedFab : extendedFabs) {
56+
extendedFab.setOnClickListener(
57+
v ->
58+
Snackbar.make(
59+
v, R.string.cat_extended_fab_clicked, BaseTransientBottomBar.LENGTH_SHORT)
60+
.show());
61+
}
62+
63+
return root;
64+
}
65+
66+
@Override
67+
public boolean shouldShowDefaultDemoActionBar() {
68+
return false;
69+
}
70+
71+
@LayoutRes
72+
protected int getExtendedFabContent() {
73+
return R.layout.cat_extended_fab_behavior_fragment;
74+
}
75+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2019 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+
* https://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 io.material.catalog.fab;
18+
19+
import io.material.catalog.R;
20+
21+
import android.os.Bundle;
22+
import androidx.annotation.LayoutRes;
23+
import androidx.annotation.Nullable;
24+
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
25+
import com.google.android.material.snackbar.BaseTransientBottomBar;
26+
import com.google.android.material.snackbar.Snackbar;
27+
import android.view.LayoutInflater;
28+
import android.view.View;
29+
import android.view.ViewGroup;
30+
import android.widget.Button;
31+
import io.material.catalog.feature.DemoFragment;
32+
import io.material.catalog.feature.DemoUtils;
33+
import java.util.List;
34+
35+
/** An Extended FAB demo fragment. */
36+
public class ExtendedFabDemoFragment extends DemoFragment {
37+
38+
private boolean fabsShown = true;
39+
private boolean fabsExpanded = true;
40+
41+
@Nullable
42+
@Override
43+
public View onCreateDemoView(
44+
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
45+
View root =
46+
layoutInflater.inflate(
47+
R.layout.cat_extended_fab_fragment, viewGroup, false /* attachToRoot */);
48+
49+
ViewGroup content = root.findViewById(R.id.content);
50+
View.inflate(getContext(), getExtendedFabContent(), content);
51+
52+
List<ExtendedFloatingActionButton> extendedFabs =
53+
DemoUtils.findViewsWithType(root, ExtendedFloatingActionButton.class);
54+
for (ExtendedFloatingActionButton extendedFab : extendedFabs) {
55+
extendedFab.setOnClickListener(
56+
v -> {
57+
Snackbar.make(v, R.string.cat_extended_fab_clicked, BaseTransientBottomBar.LENGTH_SHORT)
58+
.show();
59+
});
60+
}
61+
62+
Button showHideFabs = root.findViewById(R.id.show_hide_fabs);
63+
showHideFabs.setOnClickListener(
64+
v -> {
65+
for (ExtendedFloatingActionButton extendedFab : extendedFabs) {
66+
if (fabsShown) {
67+
extendedFab.hide();
68+
showHideFabs.setText(R.string.show_fabs_label);
69+
} else {
70+
extendedFab.show();
71+
showHideFabs.setText(R.string.hide_fabs_label);
72+
}
73+
}
74+
fabsShown = !fabsShown;
75+
});
76+
77+
Button collapseExpandFabs = root.findViewById(R.id.collapse_expand_fabs);
78+
collapseExpandFabs.setOnClickListener(
79+
v -> {
80+
for (ExtendedFloatingActionButton extendedFab : extendedFabs) {
81+
if (fabsExpanded) {
82+
extendedFab.shrink();
83+
collapseExpandFabs.setText(R.string.extend_fabs_label);
84+
} else {
85+
extendedFab.extend();
86+
collapseExpandFabs.setText(R.string.shrink_fabs_label);
87+
}
88+
}
89+
fabsExpanded = !fabsExpanded;
90+
});
91+
92+
return root;
93+
}
94+
95+
@LayoutRes
96+
protected int getExtendedFabContent() {
97+
return R.layout.mtrl_extended_fabs;
98+
}
99+
}

catalog/java/io/material/catalog/fab/FabFragment.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import io.material.catalog.feature.Demo;
2828
import io.material.catalog.feature.DemoLandingFragment;
2929
import io.material.catalog.feature.FeatureDemo;
30+
import java.util.ArrayList;
31+
import java.util.List;
3032

3133
/** A landing fragment that links to FAB demos for the Catalog app. */
3234
public class FabFragment extends DemoLandingFragment {
@@ -51,6 +53,26 @@ public Fragment createFragment() {
5153
};
5254
}
5355

56+
@Override
57+
public List<Demo> getAdditionalDemos() {
58+
List<Demo> additionalDemos = new ArrayList<>();
59+
additionalDemos.add(
60+
new Demo(R.string.cat_extended_fab_demo_title) {
61+
@Override
62+
public Fragment createFragment() {
63+
return new ExtendedFabDemoFragment();
64+
}
65+
});
66+
additionalDemos.add(
67+
new Demo(R.string.cat_extended_fab_behavior_demo_title) {
68+
@Override
69+
public Fragment createFragment() {
70+
return new ExtendedFabBehaviorDemoFragment();
71+
}
72+
});
73+
return additionalDemos;
74+
}
75+
5476
/** The Dagger module for {@link FabFragment} dependencies. */
5577
@dagger.Module
5678
public abstract static class Module {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2018 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<androidx.coordinatorlayout.widget.CoordinatorLayout
18+
xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent">
22+
<com.google.android.material.appbar.AppBarLayout
23+
android:id="@+id/app_bar"
24+
android:layout_width="match_parent"
25+
android:layout_height="192dp">
26+
<com.google.android.material.appbar.CollapsingToolbarLayout
27+
style="?attr/catalogToolbarStyle"
28+
android:layout_width="match_parent"
29+
android:layout_height="match_parent"
30+
app:collapsedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
31+
app:contentScrim="?attr/colorPrimary"
32+
app:expandedTitleGravity="bottom"
33+
app:expandedTitleMarginBottom="56dp"
34+
app:expandedTitleMarginStart="16dp"
35+
app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Headline"
36+
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
37+
38+
<androidx.appcompat.widget.Toolbar
39+
android:id="@+id/toolbar"
40+
style="?attr/catalogToolbarWithCloseButtonStyle"
41+
android:layout_width="match_parent"
42+
android:layout_height="?attr/actionBarSize"
43+
app:layout_collapseMode="pin"
44+
app:title="Collapsing Title"/>
45+
</com.google.android.material.appbar.CollapsingToolbarLayout>
46+
</com.google.android.material.appbar.AppBarLayout>
47+
48+
<androidx.core.widget.NestedScrollView
49+
android:layout_width="match_parent"
50+
android:layout_height="match_parent"
51+
app:layout_behavior="@string/appbar_scrolling_view_behavior">
52+
53+
<include layout="@layout/cat_topappbar_filler_text_view"/>
54+
</androidx.core.widget.NestedScrollView>
55+
56+
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
57+
android:layout_width="wrap_content"
58+
android:layout_height="wrap_content"
59+
android:layout_margin="8dp"
60+
android:contentDescription="@string/cat_extended_fab_content_desc"
61+
android:text="@string/extended_fab_label"
62+
app:icon="@drawable/ic_add_24px"
63+
app:layout_anchor="@id/app_bar"
64+
app:layout_anchorGravity="bottom|right|end"/>
65+
66+
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2018 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<LinearLayout
19+
xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
android:gravity="center_horizontal"
23+
android:orientation="vertical">
24+
<FrameLayout
25+
android:layout_width="wrap_content"
26+
android:layout_height="0dp"
27+
android:layout_weight="1">
28+
<ScrollView
29+
android:id="@+id/content"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:layout_gravity="center_vertical"/>
33+
</FrameLayout>
34+
35+
<LinearLayout
36+
android:layout_width="match_parent"
37+
android:layout_height="wrap_content"
38+
android:paddingStart="24dp"
39+
android:paddingEnd="24dp"
40+
android:gravity="center_vertical"
41+
android:orientation="horizontal">
42+
<Button
43+
android:id="@+id/show_hide_fabs"
44+
android:layout_width="0px"
45+
android:layout_height="wrap_content"
46+
android:layout_weight="1"
47+
android:layout_margin="8dp"
48+
android:text="@string/hide_fabs_label"/>
49+
<Button
50+
android:id="@+id/collapse_expand_fabs"
51+
android:layout_width="0px"
52+
android:layout_height="wrap_content"
53+
android:layout_weight="1"
54+
android:layout_margin="8dp"
55+
android:text="@string/shrink_fabs_label"/>
56+
</LinearLayout>
57+
</LinearLayout>

0 commit comments

Comments
 (0)