Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement context menu on key press instead of long click/tap

Tags:

android

I have a ListActivity and I want to implement context menu for each of the list elements. I know that the common way to do this is to show the context menu on long click/tap. I want to know if there is a way to show the context menu for each element on a key press(preferably the menu key). To rephrase my question, how can I trigger the context menu and not the options menu by pressing the menu key(or any other key).

like image 674
Tushar Gupta Avatar asked Jun 05 '10 18:06

Tushar Gupta


People also ask

Which menu will display after long press click on any view?

A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

Which menu appears on long press of an item of a ListView?

Which menu appears on long press of an item of a ListView? Android context menu appears when user press long click on the element. It is also known as floating menu.


2 Answers

To open the context menu, call openContextMenu(). To trigger it via a key, override onKeyDown() or onKeyUp().

That being said, I really do not recommend this.

Users complain that Android has no UI standards. Instead, Android has UI conventions, ones that allow developers some freedom (and, more importantly, are not barriers to getting your app listed on the Android Market).

However, those users' point is very valid -- their experience is marred when apps decide to go off on a UI tangent. Deciding to have the MENU key pop a context menu would be one such tangent. For starters, on touch-screen devices, this will not work very well, because there is no selected item in your ListView, so it would be unclear to the user what the MENU pertains to.

I suspect that there is a better solution for whatever problem you think you are solving this way.

like image 130
CommonsWare Avatar answered Oct 23 '22 10:10

CommonsWare


I have actually done this same thing for one of my apps because it made sense for my app. The best and easiest way to do this is to override onListItemClick() for your listActivity. This is better than onKeyDown() because it pertains specifically to the list item (view) in question and will only apply to the list items, and not the rest of your screen.

onListItemClick(ListView l, View v, int position, long id)
{
   v.showContextMenu();
}

then all you have to do override the context menu methods and you're golden

like image 41
mtmurdock Avatar answered Oct 23 '22 11:10

mtmurdock