Skip to content

Commit 7a6866c

Browse files
[Button] [SplitButton] Added PopupMenu to the SplitButton demo.
PiperOrigin-RevId: 720684004
1 parent 9302f54 commit 7a6866c

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

catalog/java/io/material/catalog/button/SplitButtonDemoFragment.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
import io.material.catalog.R;
2020

2121
import android.os.Bundle;
22+
import androidx.appcompat.widget.PopupMenu;
2223
import android.view.LayoutInflater;
2324
import android.view.View;
2425
import android.view.ViewGroup;
2526
import androidx.annotation.LayoutRes;
27+
import androidx.annotation.MenuRes;
2628
import androidx.annotation.Nullable;
29+
import com.google.android.material.button.MaterialButton;
2730
import com.google.android.material.button.MaterialSplitButton;
2831
import com.google.android.material.materialswitch.MaterialSwitch;
32+
import com.google.android.material.snackbar.Snackbar;
2933
import io.material.catalog.feature.DemoFragment;
3034
import io.material.catalog.feature.DemoUtils;
3135
import java.util.List;
@@ -52,9 +56,37 @@ public View onCreateDemoView(
5256
splitButton.setEnabled(isChecked);
5357
}
5458
});
59+
60+
// Popup menu demo for split button
61+
MaterialButton button =
62+
(MaterialButton) view.findViewById(R.id.expand_more_or_less_filled_icon_popup);
63+
button.addOnCheckedChangeListener(
64+
(buttonView, isChecked) -> {
65+
if (isChecked) {
66+
showMenu(button, R.menu.split_button_menu);
67+
}
68+
});
69+
5570
return view;
5671
}
5772

73+
@SuppressWarnings("RestrictTo")
74+
private void showMenu(View v, @MenuRes int menuRes) {
75+
PopupMenu popup = new PopupMenu(getContext(), v);
76+
// Inflating the Popup using XML file
77+
popup.getMenuInflater().inflate(menuRes, popup.getMenu());
78+
popup.setOnMenuItemClickListener(
79+
menuItem -> {
80+
Snackbar.make(
81+
getActivity().findViewById(android.R.id.content),
82+
menuItem.getTitle(),
83+
Snackbar.LENGTH_LONG)
84+
.show();
85+
return true;
86+
});
87+
popup.show();
88+
}
89+
5890
@LayoutRes
5991
protected int getSplitButtonContent() {
6092
return R.layout.cat_split_button_fragment;

catalog/java/io/material/catalog/button/res/layout/cat_split_button_fragment.xml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<com.google.android.material.button.MaterialSplitButton
6666
android:layout_width="wrap_content"
6767
android:layout_height="wrap_content">
68-
<Button
68+
<com.google.android.material.button.MaterialButton
6969
android:layout_width="wrap_content"
7070
android:layout_height="wrap_content"
7171
android:paddingTop="6dp"
@@ -200,6 +200,40 @@
200200
app:icon="@drawable/m3_split_button_chevron_avd"/>
201201
</com.google.android.material.button.MaterialSplitButton>
202202

203+
<TextView
204+
android:layout_width="wrap_content"
205+
android:layout_height="wrap_content"
206+
android:paddingTop="16dp"
207+
android:paddingBottom="8dp"
208+
android:text="@string/cat_split_button_subheader_popup_menu"
209+
android:textAppearance="?attr/textAppearanceSubtitle1"/>
210+
<com.google.android.material.button.MaterialSplitButton
211+
android:layout_width="wrap_content"
212+
android:layout_height="wrap_content">
213+
<com.google.android.material.button.MaterialButton
214+
android:layout_width="wrap_content"
215+
android:layout_height="wrap_content"
216+
android:paddingTop="6dp"
217+
android:paddingBottom="6dp"
218+
android:paddingStart="@dimen/m3_comp_split_button_small_leading_button_leading_space"
219+
android:paddingEnd="@dimen/m3_comp_split_button_small_leading_button_trailing_space"
220+
android:minWidth="@dimen/mtrl_min_touch_target_size"
221+
android:contentDescription="@string/cat_split_button_label_edit"
222+
app:icon="@drawable/ic_edit_vd_theme_24dp"
223+
app:iconSize="20dp"
224+
app:iconPadding="0dp"/>
225+
<com.google.android.material.button.MaterialButton
226+
style="?attr/materialSplitButtonIconFilledStyle"
227+
android:id="@+id/expand_more_or_less_filled_icon_popup"
228+
android:layout_width="wrap_content"
229+
android:layout_height="wrap_content"
230+
android:paddingStart="14dp"
231+
android:paddingEnd="14dp"
232+
android:minWidth="@dimen/mtrl_min_touch_target_size"
233+
android:contentDescription="@string/cat_split_button_label_chevron"
234+
app:icon="@drawable/m3_split_button_chevron_avd"/>
235+
</com.google.android.material.button.MaterialSplitButton>
236+
203237
<com.google.android.material.materialswitch.MaterialSwitch
204238
android:id="@+id/switch_enable"
205239
android:paddingTop="16dp"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
<menu xmlns:android="http://schemas.android.com/apk/res/android">
19+
<item
20+
android:id="@+id/one"
21+
android:title="@string/cat_split_button_popup_menu_item_one"/>
22+
<item
23+
android:id="@+id/two"
24+
android:title="@string/cat_split_button_popup_menu_item_two"/>
25+
<item
26+
android:id="@+id/three"
27+
android:title="@string/cat_split_button_popup_menu_item_three"/>
28+
</menu>

catalog/java/io/material/catalog/button/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
<string name="cat_multi_select">Multi-select</string>
8888
<string name="cat_split_button_subheader_filled" translatable="false">Split button, primary</string>
8989
<string name="cat_split_button_subheader_filled_tonal" translatable="false">Split button, secondary</string>
90+
<string name="cat_split_button_subheader_popup_menu" translatable="false">Split button, popup menu</string>
91+
<string name="cat_split_button_popup_menu_item_one" translatable="false">List item 1</string>
92+
<string name="cat_split_button_popup_menu_item_two" translatable="false">List item 2</string>
93+
<string name="cat_split_button_popup_menu_item_three" translatable="false">List item 3</string>
9094
<string name="cat_icon_only" description="A label for icon only buttons below. [CHAR LIMIT=NONE]">Icon only</string>
9195
<string name="cat_icon_only_toggle" description="A label for icon only toggle buttons below. [CHAR LIMIT=NONE]">Icon only toggle</string>
9296
<string name="cat_label_only" description="A label for lebal only buttons below. [CHAR LIMIT=NONE]">Label only</string>

0 commit comments

Comments
 (0)