Tushar Gupta
Tushar Gupta

Reputation: 1410

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

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).

Upvotes: 9

Views: 8604

Answers (2)

mtmurdock
mtmurdock

Reputation: 13062

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

Upvotes: 7

CommonsWare
CommonsWare

Reputation: 1006584

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.

Upvotes: 7

Related Questions