Skip to content

Commit a82391a

Browse files
imhappidrchen
authored andcommitted
[Catalog] Create docked toolbar demo in M3 catalog
PiperOrigin-RevId: 724205383
1 parent 51873bb commit a82391a

11 files changed

+498
-1
lines changed

catalog/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def srcDirs = [
7171
'dialog',
7272
'divider',
7373
'draggable',
74+
'dockedtoolbar',
7475
'elevation',
7576
'imageview',
7677
'fab',
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
Copyright 2025 The Android Open Source Project
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
14+
android:width="24dp"
15+
android:height="24dp"
16+
android:viewportWidth="960"
17+
android:viewportHeight="960"
18+
android:tint="?attr/colorControlNormal"
19+
android:autoMirrored="true">
20+
<path
21+
android:fillColor="@android:color/white"
22+
android:pathData="M313,520L537,744L480,800L160,480L480,160L537,216L313,440L800,440L800,520L313,520Z"/>
23+
</vector>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
Copyright 2025 The Android Open Source Project
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
14+
android:width="24dp"
15+
android:height="24dp"
16+
android:viewportWidth="960"
17+
android:viewportHeight="960"
18+
android:tint="?attr/colorControlNormal"
19+
android:autoMirrored="true">
20+
<path
21+
android:fillColor="@android:color/white"
22+
android:pathData="M647,520L160,520L160,440L647,440L423,216L480,160L800,480L480,800L423,744L647,520Z"/>
23+
</vector>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--
2+
Copyright 2025 The Android Open Source Project
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
14+
android:width="24dp"
15+
android:height="24dp"
16+
android:viewportWidth="960"
17+
android:viewportHeight="960"
18+
android:tint="?attr/colorControlNormal">
19+
<path
20+
android:fillColor="@android:color/white"
21+
android:pathData="M160,720L800,720Q800,720 800,720Q800,720 800,720L800,400L520,400L520,240L160,240Q160,240 160,240Q160,240 160,240L160,720Q160,720 160,720Q160,720 160,720ZM160,800Q127,800 103.5,776.5Q80,753 80,720L80,240Q80,207 103.5,183.5Q127,160 160,160L800,160Q833,160 856.5,183.5Q880,207 880,240L880,720Q880,753 856.5,776.5Q833,800 800,800L160,800ZM160,720Q160,720 160,720Q160,720 160,720L160,240Q160,240 160,240Q160,240 160,240L160,240Q160,240 160,240Q160,240 160,240L160,720Q160,720 160,720Q160,720 160,720Z"/>
22+
</vector>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2024 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+
package io.material.catalog.dockedtoolbar;
17+
18+
import io.material.catalog.R;
19+
20+
import androidx.fragment.app.Fragment;
21+
import androidx.annotation.NonNull;
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 fragment that displays Docked Toolbar demos for the Catalog app. */
32+
public class DockedToolbarFragment extends DemoLandingFragment {
33+
@Override
34+
public int getTitleResId() {
35+
return R.string.cat_docked_toolbar_title;
36+
}
37+
38+
@Override
39+
public int getDescriptionResId() {
40+
return R.string.cat_docked_toolbar_description;
41+
}
42+
43+
@Override
44+
@NonNull
45+
public Demo getMainDemo() {
46+
return new Demo() {
47+
@Override
48+
public Fragment createFragment() {
49+
return new DockedToolbarMainDemoFragment();
50+
}
51+
};
52+
}
53+
54+
/** The Dagger module for {@link DockedToolbarFragment} dependencies. */
55+
@dagger.Module
56+
public abstract static class Module {
57+
@FragmentScope
58+
@ContributesAndroidInjector
59+
abstract DockedToolbarFragment contributeInjector();
60+
61+
@IntoSet
62+
@Provides
63+
@ActivityScope
64+
static FeatureDemo provideFeatureDemo() {
65+
return new FeatureDemo(R.string.cat_docked_toolbar_title, R.drawable.ic_bottomnavigation) {
66+
@Override
67+
public Fragment createFragment() {
68+
return new DockedToolbarFragment();
69+
}
70+
};
71+
}
72+
}
73+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
package io.material.catalog.dockedtoolbar;
17+
18+
import io.material.catalog.R;
19+
20+
import android.os.Bundle;
21+
import androidx.appcompat.app.AppCompatActivity;
22+
import androidx.appcompat.widget.PopupMenu;
23+
import androidx.appcompat.widget.Toolbar;
24+
import android.view.LayoutInflater;
25+
import android.view.View;
26+
import android.view.ViewGroup;
27+
import android.widget.Button;
28+
import androidx.annotation.LayoutRes;
29+
import androidx.annotation.MenuRes;
30+
import androidx.annotation.NonNull;
31+
import androidx.annotation.Nullable;
32+
import com.google.android.material.dockedtoolbar.DockedToolbarLayout;
33+
import com.google.android.material.snackbar.Snackbar;
34+
import io.material.catalog.feature.DemoFragment;
35+
36+
/** A fragment that displays the main Docked Toolbar demo for the Catalog app. */
37+
public class DockedToolbarMainDemoFragment extends DemoFragment {
38+
39+
private DockedToolbarLayout dockedToolbar;
40+
41+
@NonNull
42+
@Override
43+
public View onCreateDemoView(
44+
@NonNull LayoutInflater layoutInflater,
45+
@Nullable ViewGroup viewGroup,
46+
@Nullable Bundle bundle) {
47+
48+
View view = layoutInflater.inflate(getLayoutResId(), viewGroup, /* attachToRoot= */ false);
49+
Toolbar toolbar = view.findViewById(R.id.toolbar);
50+
dockedToolbar = view.findViewById(R.id.docked_toolbar);
51+
((AppCompatActivity) requireActivity()).setSupportActionBar(toolbar);
52+
53+
Button leftArrowButton = view.findViewById(R.id.docked_toolbar_left_arrow_button);
54+
Button rightArrowButton = view.findViewById(R.id.docked_toolbar_right_arrow_button);
55+
Button addButton = view.findViewById(R.id.docked_toolbar_add_button);
56+
Button tabButton = view.findViewById(R.id.docked_toolbar_tab_button);
57+
setupSnackbarOnClick(leftArrowButton);
58+
setupSnackbarOnClick(rightArrowButton);
59+
setupSnackbarOnClick(addButton);
60+
setupSnackbarOnClick(tabButton);
61+
62+
Button overflowClick = view.findViewById(R.id.docked_toolbar_button_overflow_button);
63+
64+
overflowClick.setOnClickListener(v -> showMenu(v, R.menu.overflow_menu));
65+
66+
return view;
67+
}
68+
69+
private void showMenu(View v, @MenuRes int menuRes) {
70+
PopupMenu popup = new PopupMenu(getContext(), v);
71+
// Inflating the Popup using xml file
72+
popup.getMenuInflater().inflate(menuRes, popup.getMenu());
73+
popup.setOnMenuItemClickListener(
74+
menuItem -> {
75+
Snackbar.make(
76+
dockedToolbar,
77+
menuItem.getTitle(),
78+
Snackbar.LENGTH_SHORT)
79+
.setAnchorView(dockedToolbar)
80+
.show();
81+
return true;
82+
});
83+
popup.show();
84+
}
85+
86+
private void setupSnackbarOnClick(View view) {
87+
view.setOnClickListener(
88+
v ->
89+
Snackbar.make(
90+
dockedToolbar,
91+
view.getContentDescription(),
92+
Snackbar.LENGTH_SHORT)
93+
.setAnchorView(dockedToolbar)
94+
.show());
95+
}
96+
97+
@LayoutRes
98+
protected int getLayoutResId() {
99+
return R.layout.cat_docked_toolbar_fragment;
100+
}
101+
102+
@Override
103+
public boolean shouldShowDefaultDemoActionBar() {
104+
return false;
105+
}
106+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 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+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:app="http://schemas.android.com/apk/res-auto"
19+
android:baselineAligned="false" android:orientation="horizontal"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:gravity="center">
23+
24+
<FrameLayout
25+
android:layout_width="0dp"
26+
android:layout_height="wrap_content"
27+
android:layout_weight="1">
28+
<Button
29+
android:id="@+id/docked_toolbar_left_arrow_button"
30+
android:layout_width="48dp"
31+
android:layout_height="48dp"
32+
android:layout_gravity="center"
33+
style="?attr/materialIconButtonStyle"
34+
android:contentDescription="@string/cat_docked_toolbar_left_arrow_button_description"
35+
app:icon="@drawable/ic_arrow_back_24px" />
36+
</FrameLayout>
37+
38+
<FrameLayout
39+
android:layout_width="0dp"
40+
android:layout_height="wrap_content"
41+
android:layout_weight="1">
42+
<Button
43+
android:id="@+id/docked_toolbar_right_arrow_button"
44+
android:layout_width="48dp"
45+
android:layout_height="48dp"
46+
android:layout_gravity="center"
47+
style="?attr/materialIconButtonStyle"
48+
android:contentDescription="@string/cat_docked_toolbar_right_arrow_button_description"
49+
app:icon="@drawable/ic_arrow_forward_24px" />
50+
</FrameLayout>
51+
52+
<FrameLayout
53+
android:layout_width="0dp"
54+
android:layout_height="wrap_content"
55+
android:layout_weight="1">
56+
<Button
57+
android:id="@+id/docked_toolbar_add_button"
58+
android:layout_width="48dp"
59+
android:layout_height="48dp"
60+
android:layout_gravity="center"
61+
android:gravity="center"
62+
android:paddingVertical="8dp"
63+
android:paddingHorizontal="8dp"
64+
app:iconSize="24dp"
65+
style="?attr/materialIconButtonFilledStyle"
66+
app:shapeAppearance="?attr/shapeAppearanceCornerMedium"
67+
android:contentDescription="@string/cat_docked_toolbar_add_button_description"
68+
app:icon="@drawable/ic_add_24px" />
69+
</FrameLayout>
70+
71+
<FrameLayout
72+
android:layout_width="0dp"
73+
android:layout_height="wrap_content"
74+
android:layout_weight="1">
75+
<Button
76+
android:id="@+id/docked_toolbar_tab_button"
77+
android:layout_width="48dp"
78+
android:layout_height="48dp"
79+
android:layout_gravity="center"
80+
style="?attr/materialIconButtonStyle"
81+
android:contentDescription="@string/cat_docked_toolbar_tab_button_description"
82+
app:icon="@drawable/ic_tab_24px" />
83+
</FrameLayout>
84+
85+
<FrameLayout
86+
android:layout_width="0dp"
87+
android:layout_height="wrap_content"
88+
android:layout_weight="1">
89+
<Button
90+
android:id="@+id/docked_toolbar_button_overflow_button"
91+
android:layout_width="40dp"
92+
android:layout_height="48dp"
93+
android:layout_gravity="center"
94+
style="?attr/materialIconButtonStyle"
95+
android:contentDescription="@string/cat_docked_toolbar_overflow_button_description"
96+
app:icon="@drawable/abc_ic_menu_overflow_material" />
97+
</FrameLayout>
98+
99+
</LinearLayout>

0 commit comments

Comments
 (0)