Respoding to Menu button click?

by MBraude » Wed, 25 Jun 2008 07:52:09 GMT

Sponsored Links
 Hi Everybody,

I'm sure this is an easy thing to do, but I haven't run into an
example yet that responds to the user clicking the Menu button on the
phone.  I'd like to be able to show / hide some things based on this
click event.  What's the easiest way to do this?

Thanks very much,


Respoding to Menu button click?

by Mark Murphy » Wed, 25 Jun 2008 11:19:56 GMT


The Menu button pops up the options menu for the activity. To fill in 
that menu, you need to implement onCreateOptionsMenu() in your activity 
and populate the supplied Menu object, per the docs: in the SDK's ApiDemos shows the use of the options 
menu. I'm sure there are any number of tutorials up on as 
well. The code bundle at also has sample  
code for both the options menu and the context menu (a.k.a., "long tap" 

I think there was an earlier post in one of these Google Groups from a 
Googler that indicated using the Menu button for anything else other 
than the options menu is uncool.

Mark Murphy (a Commons Guy) 
_The Busy Coder's Guide to Android Development_ -- Available Now!


Sponsored Links

Respoding to Menu button click?

by Ashok Singal » Wed, 25 Jun 2008 19:17:36 GMT

 Hi Mike,

If you could go through the Tutorial Exercise 2nd, you will find that there
are two functions that Android has provided for managing items in the Menu
Control as following:

1. onCreateOptionsMenu(): You can create your items in Menu.

2. onMenuItemSelected(): What action needs to be taken when a particular
menu item is selected? This function gets an Item parameter so when you will
call item.getID(), it will return a particular menu item selected by the
user and you can handle that accordingly.

I hope that this answers to your question.



Other Threads

1. Speed difference between DOM and SAX parser for small documents


currently, we use the SAX parser bundled with Android to parse XML
coming from our servers. We found however that parsing the XML using a
SAX handler is prone to errors and difficult to maintain. Therefore I
thought about switiching to a DOM parser, and probably also a wrapper
around it (like JDOM) for convenience.

But, does anyone know how big the performance impact will be? Is it
negligible for documents that are merely 10-50k in size? I remember
that in early alphas of Android, the DOM parser was noticeably slower,
but I also heard that this has become better meanwhile.


2. Problem when updating ListView

Hi all,

I have a ListView with a footer View that is bind to a custom
ArrayAdapter within a ListActivity. When creating the activity, I fill
in the ArrayAdapter with some items through a dedicated thread. At the
end, the thread calls setListAdapter and requestFocus through a
handler. At this point, everything works fine. I mean, when scrolling
the ListView, all items get hilighted correctly, even when the footer
view gets focused. Now, I need to update the items in the ListView by
adding a few items more. Again, I use a thread to update the
ArrayAdapter and use a handler to post invalidate() on the ListView.
Now, when I scroll down the ListView and reach the footer view, the
last item of the list gets unreadable when losing focus. The same
happens with the first item of the list when reaching a header view or
a parent tab. I tried requestFocus() again but it didn't work. I also
tried android:drawSelectorOnTop="false" with no more result.

Any guess? Help is welcomed.


3. iPhone Flops in India

4. How to update the SDK

5. ContentProvider instance

6. Building a .so lib

7. emulator support in 2.6.27 ?