It is all very well to be able to design menus and have them inflate according to user actions but we also need some way to interpret and respond in return. Again the Activity class provides a hook for menu selections in the form of a callback, onOptionsItemSelected()
.
In this recipe we will be using an XML menu definition identical to the previous one, so you may well wish to copy and paste this file to save time. Nevertheless start up a new Android project in Eclipse.
Define an XML menu called
my_menu.xml
in theres/menu
folder, creating this folder if necessary, and provide it with two items, giving each at least anid
and atitle
.In the
res/layout
folder edit themain.xml
file so that the layout contains a single TextView with anandroid:id
, for example:<TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dip" android:text="Press menu" />
In...