[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
This commit is contained in:
Material Design Team 2025-09-04 07:50:49 +00:00 committed by Hunter Stich
parent 59f0fc5f41
commit 4852ebcf2f
2 changed files with 17 additions and 2 deletions

View File

@ -36,6 +36,7 @@ import android.text.style.BackgroundColorSpan;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -90,7 +91,7 @@ public class MenuMainDemoFragment extends DemoFragment {
iconMenuButton.setOnClickListener(v -> showMenu(v, R.menu.menu_with_icons));
TextView contextMenuTextView = view.findViewById(R.id.context_menu_tv);
registerForContextMenu(contextMenuTextView);
setupContextMenuWithKeyboard(contextMenuTextView);
Button listPopupWindowButton = view.findViewById(R.id.list_popup_window);
ListPopupWindow listPopupWindow = initializeListPopupMenu(listPopupWindowButton);
@ -99,6 +100,20 @@ public class MenuMainDemoFragment extends DemoFragment {
return view;
}
private void setupContextMenuWithKeyboard(View view) {
registerForContextMenu(view);
view.setOnKeyListener(
(v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.isCtrlPressed()
&& (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) {
v.showContextMenu();
return true;
}
return false;
});
}
@SuppressWarnings("RestrictTo")
private void showMenu(View v, @MenuRes int menuRes) {
PopupMenu popup = new PopupMenu(getContext(), v);

View File

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