Skip to content

Commit 4852ebc

Browse files
Material Design Teamhunterstich
authored andcommitted
[Menu][A11y] Update Menu Catalog Demo to use Ctrl + Enter for Context Menu
The context menu in the Menu catalog demo now opens when `Ctrl + Enter` is pressed on a keyboard, instead of just `Enter`. The description text has also been updated to reflect this change. PiperOrigin-RevId: 802901209
1 parent 59f0fc5 commit 4852ebc

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

catalog/java/io/material/catalog/menu/MenuMainDemoFragment.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import android.util.TypedValue;
3737
import android.view.ContextMenu;
3838
import android.view.ContextMenu.ContextMenuInfo;
39+
import android.view.KeyEvent;
3940
import android.view.LayoutInflater;
4041
import android.view.Menu;
4142
import android.view.MenuInflater;
@@ -90,7 +91,7 @@ public View onCreateDemoView(
9091
iconMenuButton.setOnClickListener(v -> showMenu(v, R.menu.menu_with_icons));
9192

9293
TextView contextMenuTextView = view.findViewById(R.id.context_menu_tv);
93-
registerForContextMenu(contextMenuTextView);
94+
setupContextMenuWithKeyboard(contextMenuTextView);
9495

9596
Button listPopupWindowButton = view.findViewById(R.id.list_popup_window);
9697
ListPopupWindow listPopupWindow = initializeListPopupMenu(listPopupWindowButton);
@@ -99,6 +100,20 @@ public View onCreateDemoView(
99100
return view;
100101
}
101102

103+
private void setupContextMenuWithKeyboard(View view) {
104+
registerForContextMenu(view);
105+
view.setOnKeyListener(
106+
(v, keyCode, event) -> {
107+
if (event.getAction() == KeyEvent.ACTION_DOWN
108+
&& event.isCtrlPressed()
109+
&& (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) {
110+
v.showContextMenu();
111+
return true;
112+
}
113+
return false;
114+
});
115+
}
116+
102117
@SuppressWarnings("RestrictTo")
103118
private void showMenu(View v, @MenuRes int menuRes) {
104119
PopupMenu popup = new PopupMenu(getContext(), v);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</string>
3232

3333
<string name="menu_context_menu_description" description="Text explaining how to use context menus [CHAR_LIMIT=NONE].">
34-
This TextView opens a context menu when long pressed or by pressing Enter on a keyboard.
34+
This TextView opens a context menu when long pressed or by pressing Ctrl + Enter on a keyboard.
3535
\n\nIt provides actions that affect the selected content or context frame.
3636
</string>
3737
<string name="context_menu_highlight" description="text for a menu option to highlight text [CHAR_LIMIT=20].">Highlight</string>

0 commit comments

Comments
 (0)