Skip to content

Commit d24edd1

Browse files
hunterstichdsn5ft
authored andcommitted
[Snackbar] Added snackbar catalog demo
Resolves #4939 PiperOrigin-RevId: 816329734
1 parent ee52adb commit d24edd1

7 files changed

Lines changed: 338 additions & 0 deletions

File tree

catalog/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def srcDirs = [
113113
'shapetheming',
114114
'sidesheet',
115115
'slider',
116+
'snackbar',
116117
'tableofcontents',
117118
'tabs',
118119
'textfield',
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2025 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 io.material.catalog.snackbar;
18+
19+
import io.material.catalog.R;
20+
21+
import androidx.fragment.app.Fragment;
22+
import dagger.Provides;
23+
import dagger.android.ContributesAndroidInjector;
24+
import dagger.multibindings.IntoSet;
25+
import io.material.catalog.application.scope.ActivityScope;
26+
import io.material.catalog.application.scope.FragmentScope;
27+
import io.material.catalog.feature.Demo;
28+
import io.material.catalog.feature.DemoLandingFragment;
29+
import io.material.catalog.feature.FeatureDemo;
30+
31+
/** A landing fragment that links to Snackbar demos for the Catalog app. */
32+
public class SnackbarFragment extends DemoLandingFragment {
33+
34+
@Override
35+
public int getTitleResId() {
36+
return R.string.cat_snackbar_title;
37+
}
38+
39+
@Override
40+
public int getDescriptionResId() {
41+
return R.string.cat_snackbar_description;
42+
}
43+
44+
@Override
45+
public Demo getMainDemo() {
46+
return new Demo() {
47+
@Override
48+
public Fragment createFragment() {
49+
return new SnackbarMainDemoFragment();
50+
}
51+
};
52+
}
53+
54+
/** The Dagger module for {@link SnackbarFragment} dependencies. */
55+
@dagger.Module
56+
public abstract static class Module {
57+
58+
@FragmentScope
59+
@ContributesAndroidInjector
60+
abstract SnackbarFragment contributeInjector();
61+
62+
@IntoSet
63+
@Provides
64+
@ActivityScope
65+
static FeatureDemo provideFeatureDemo() {
66+
return new FeatureDemo(R.string.cat_snackbar_title, R.drawable.ic_placeholder) {
67+
@Override
68+
public Fragment createFragment() {
69+
return new SnackbarFragment();
70+
}
71+
};
72+
}
73+
}
74+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2025 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 io.material.catalog.snackbar;
18+
19+
import io.material.catalog.R;
20+
21+
import android.content.Context;
22+
import android.os.Bundle;
23+
import android.view.ContextThemeWrapper;
24+
import android.view.LayoutInflater;
25+
import android.view.View;
26+
import android.view.ViewGroup;
27+
import androidx.annotation.NonNull;
28+
import androidx.annotation.Nullable;
29+
import androidx.coordinatorlayout.widget.CoordinatorLayout;
30+
import com.google.android.material.snackbar.Snackbar;
31+
import io.material.catalog.feature.DemoFragment;
32+
33+
/** A fragment that displays the main Snackbar demos for the Catalog app. */
34+
public class SnackbarMainDemoFragment extends DemoFragment {
35+
36+
@Override
37+
public View onCreateDemoView(
38+
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
39+
return layoutInflater.inflate(R.layout.cat_snackbar_main_demo_fragment, viewGroup, false);
40+
}
41+
42+
@Override
43+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
44+
super.onViewCreated(view, savedInstanceState);
45+
CoordinatorLayout coordinatorLayout = view.findViewById(R.id.coordinator_layout);
46+
47+
// Default snackbar
48+
view.findViewById(R.id.default_button)
49+
.setOnClickListener(
50+
v -> {
51+
Snackbar.make(
52+
coordinatorLayout,
53+
R.string.cat_snackbar_default_message,
54+
Snackbar.LENGTH_SHORT)
55+
.show();
56+
});
57+
58+
// Snackbar with single line
59+
view.findViewById(R.id.single_line_button)
60+
.setOnClickListener(
61+
v -> {
62+
Snackbar.make(
63+
coordinatorLayout,
64+
R.string.cat_snackbar_single_line_message,
65+
Snackbar.LENGTH_SHORT)
66+
.setTextMaxLines(1)
67+
.show();
68+
});
69+
70+
// Snackbar with action
71+
view.findViewById(R.id.with_action_button)
72+
.setOnClickListener(
73+
v -> {
74+
Snackbar.make(
75+
coordinatorLayout,
76+
R.string.cat_snackbar_with_action_message,
77+
Snackbar.LENGTH_SHORT)
78+
.setAction(R.string.cat_snackbar_action_title, a -> {})
79+
.show();
80+
});
81+
82+
// Snackbar with multiple lines
83+
view.findViewById(R.id.multi_line_button)
84+
.setOnClickListener(
85+
v -> {
86+
Snackbar.make(
87+
coordinatorLayout,
88+
R.string.cat_snackbar_multi_line_message,
89+
Snackbar.LENGTH_SHORT)
90+
.setAction(R.string.cat_snackbar_action_title, a -> {})
91+
.setTextMaxLines(5)
92+
.show();
93+
});
94+
95+
// Snackbar with custom shape
96+
view.findViewById(R.id.custom_shape_button)
97+
.setOnClickListener(
98+
v -> {
99+
// Set a custom shape by updating the theme's snackbarStyle attribute to a
100+
// custom style with a custom shapeAppearance. For demonstration, this is done
101+
// on a single snackbar using a theme overlay but it is more typically done by
102+
// setting the snackbarStyle on th main theme and customizing all snackbars app-wide.
103+
Context c =
104+
new ContextThemeWrapper(
105+
v.getContext(), R.style.ThemeOverlay_Catalog_SnackbarWithCustomShape);
106+
Snackbar.make(
107+
c,
108+
coordinatorLayout,
109+
getString(R.string.cat_snackbar_custom_shape_message),
110+
Snackbar.LENGTH_SHORT)
111+
.setAction("Done", a -> {})
112+
.show();
113+
});
114+
}
115+
}
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 2025 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+
http://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+
<androidx.coordinatorlayout.widget.CoordinatorLayout
19+
xmlns:android="http://schemas.android.com/apk/res/android"
20+
xmlns:app="http://schemas.android.com/apk/res-auto"
21+
android:id="@+id/coordinator_layout"
22+
android:layout_width="match_parent"
23+
android:layout_height="match_parent">
24+
25+
<LinearLayout
26+
android:layout_width="match_parent"
27+
android:layout_height="match_parent"
28+
android:orientation="vertical"
29+
android:gravity="center_horizontal">
30+
<Button
31+
android:id="@+id/default_button"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:text="@string/cat_snackbar_default_button"/>
35+
<Button
36+
android:id="@+id/single_line_button"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:text="@string/cat_snackbar_single_line_button"/>
40+
<Button
41+
android:id="@+id/with_action_button"
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
android:text="@string/cat_snackbar_with_action_button"/>
45+
<Button
46+
android:id="@+id/multi_line_button"
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
android:text="@string/cat_snackbar_multi_line_button"/>
50+
<Button
51+
android:id="@+id/custom_shape_button"
52+
android:layout_width="wrap_content"
53+
android:layout_height="wrap_content"
54+
android:text="@string/cat_snackbar_custom_shape_button"/>
55+
</LinearLayout>
56+
57+
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 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+
http://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+
<resources>
19+
20+
<string name="cat_snackbar_title" description=" The title of the snackbar catalog demo. [CHAR LIMIT=NONE]">
21+
Snackbar
22+
</string>
23+
<string name="cat_snackbar_description" description="The description of the snackbar catalog demo. [CHAR LIMIT=NONE]">
24+
Snackbars show short updates about app processes at the bottom of the screen
25+
</string>
26+
27+
<string name="cat_snackbar_default_button" description="The label of the button that launches a default snackbar. [CHAR LIMIT=NONE]">
28+
Default snackbar
29+
</string>
30+
<string name="cat_snackbar_default_message" description="The message of the default snackbar. [CHAR LIMIT=NONE]">
31+
This is a snackbar
32+
</string>
33+
34+
<string name="cat_snackbar_single_line_button" description="The label of the button that launches a single-line snackbar. [CHAR LIMIT=NONE]">
35+
Single-line snackbar
36+
</string>
37+
<string name="cat_snackbar_single_line_message" description="The message of the single-line snackbar. [CHAR LIMIT=NONE]">
38+
This is a single-line snackbar that has a long message that should be ellispized.
39+
</string>
40+
41+
<string name="cat_snackbar_with_action_button" description="The label of the button that launches a snackbar with an action. [CHAR LIMIT=NONE]">
42+
Snackbar with action
43+
</string>
44+
<string name="cat_snackbar_with_action_message" description="The message of the snackbar with an action. [CHAR LIMIT=NONE]">
45+
Snackbar with action
46+
</string>
47+
<string name="cat_snackbar_action_title" description="The label of the action button inside the snackbar. [CHAR LIMIT=NONE]">
48+
Done
49+
</string>
50+
51+
<string name="cat_snackbar_multi_line_button" description="The label of the button that launches a multi-line snackbar. [CHAR LIMIT=NONE]">
52+
Multi-line snackbar
53+
</string>
54+
<string name="cat_snackbar_multi_line_message" description="The message of the multi-line snackbar. [CHAR LIMIT=NONE]">
55+
This is a multi-line snackbar that has a long message which should wrap onto more than one line inside the snackbar.
56+
</string>
57+
58+
<string name="cat_snackbar_custom_shape_button" description="The label of the button that launches a snackbar with a custom background shape. [CHAR LIMIT=NONE]">
59+
Custom shape snackbar
60+
</string>
61+
<string name="cat_snackbar_custom_shape_message" description="The message of the snackbar with a custom background shape. [CHAR LIMIT=NONE]">
62+
This is a snackbar with a custom shape
63+
</string>
64+
</resources>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2025 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+
http://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+
<resources>
19+
<style name="ThemeOverlay.Catalog.SnackbarWithCustomShape" parent="Theme.Catalog">
20+
<item name="snackbarStyle">@style/Widget.Catalog.Snackbar.PillShape</item>
21+
</style>
22+
<style name="Widget.Catalog.Snackbar.PillShape" parent="Widget.Material3.Snackbar">
23+
<item name="shapeAppearance">@style/ShapeAppearance.Material3.Corner.Full</item>
24+
</style>
25+
</resources>

catalog/java/io/material/catalog/tableofcontents/TocModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import io.material.catalog.shapetheming.ShapeThemingFragment;
5252
import io.material.catalog.sidesheet.SideSheetFragment;
5353
import io.material.catalog.slider.SliderFragment;
54+
import io.material.catalog.snackbar.SnackbarFragment;
5455
import io.material.catalog.tabs.TabsFragment;
5556
import io.material.catalog.textfield.TextFieldFragment;
5657
import io.material.catalog.timepicker.TimePickerDemoLandingFragment;
@@ -90,6 +91,7 @@
9091
ShapeThemingFragment.Module.class,
9192
SideSheetFragment.Module.class,
9293
SliderFragment.Module.class,
94+
SnackbarFragment.Module.class,
9395
SwitchFragment.Module.class,
9496
TabsFragment.Module.class,
9597
TextFieldFragment.Module.class,

0 commit comments

Comments
 (0)