Android menu item tutorial


Options Menu Android Developer Tutorial

Nearly every application will require a menu to be able to facilitate a person to do actions around the application. In Android you will find three kinds of menus possible.

The Choices menu is the one which seems whenever a user touches the menu button around the mobile. This really is something that’s connected by having an activity. In 3. and then, this will come in the experience Bar itself for convenient access.

In the following paragraphs, I’m showing how you can create an Options Menu for products getting Android 2.3 or below.

The Context Menu is really a floating listing of menu products that seems whenever a user touches and holds a specific item displayed within the view, with a menu connected by using it.

The Sub Menu is really a floating listing of menu products that seems once the user touches a menu item that consists of a nested menu.

You will find two methods for creating an Options Menu inside your application. The first is by instantiating the Menu class and also the other is as simple as blowing up a Menu from an XML menu resource. According to guidelines it is usually easier to define the Menu within an XML and inflate it inside your code.

Now, let’s begin with the example.

I will just define 3 menu products within the XML. Inflate it within my code. So when a person clicks the menu products, I simply Toast a note on which continues to be clicked on.

NOTE: This really is as always not really a practically helpful piece, but adhering to my style, I wish to ensure that it stays as uncluttered and as easy as possible to ensure that the training happens easily. And also the focus is just on which concept we are attempting to learn.

So, here’s my options_menu.xml that’s produced within the res/menu folder:

android:title=”@string/next” />
android:title=”@string/previous” />
android:title=”@string/list” />

The thing is the Menu root node includes 3 item leaf nodes. Each one of the products includes an id, icon and title. The resource id is exclusive to that particular item also it enables the applying to identify which item continues to be clicked on through the user. The icon is really a drawable which should appear in the res/drawable folder and it is the main one proven within the menu item. The string may be the item’s title.

The above mentioned assumes you have three images ic_next, ic_previous and ic_list replicated in to the drawable folder. It’s understandable these image dimensions ought to be stored no more than possible.
Once this really is ready, we’ll produce a class known as ViewOptionsMenu. It’s onCreate(&hellip) method will be a simple one calling the super method and exhibiting the information as proven below.

public void onCreate(Bundle savedInstanceState) primary)

The primary.xml just shows the content: &ldquoClick around the Options Menu to see the accessible Menu Options&rdquo. This message according to standard is determined within the strings.xml file that is available within the res/values folder. Listed here are the items in the primary.xml

android:textSize=”20sp” android:textStyle=”bold” android:capitalize=”none” android:typeface=”sans”/>

Now, I have to override the technique: onCreateOptionsMenu(Menu menu). This process is known as by Android the very first time the game is loaded. This really is so for Android 2.3 and below. This is actually the code:

public boolean onCreateOptionsMenu(Menu menu)

This process gets a handle towards the MenuInflater and taking advantage of it to inflate the choices menu we have defined earlier in options_menu.xml within the res/menu folder. That’s it. The Menu is produced. Is not is really simple?

Now, the menu is produced, how can we react to the consumer as he clicks the menu. This is accomplished by overriding the onOptionsItemSelected(MenuItem item) method within the Activity itself as proven below:

public boolean onOptionsItemSelected(MenuItem item) situation
Toast.makeText(this, “You’ve selected the ” + getResources().getString( + ” menu option”,
return true
return super.onOptionsItemSelected(item)

Here, the Android system refers to this as method by passing a handle towards the MenuItem that’s been clicked on through the user. So, in this particular method, I check which item continues to be clicked on by locating the item id through item.getItemId(). I Quickly make use of the switch statement to do something in line with the id. If it’s that’s been selected, only then do we toast a note the &ldquoNext&rdquo menu option continues to be selected. In tangible applications, it’s here that you could call the right way in which should do something based on the menu clicked on.

Similarly I toast messages for &ldquoprevious&rdquo in addition to &ldquolist&rdquo menus. The entire code is downloadable here.

Begin to see the images below based on how the choices menu seems
and just what occurs when you click the options menu:
The entire of options menu works slightly in a different way for Android 3. and above.

And when you need to alter the menu options, at runtime, you have to override the onPrepareMenuOptions() method. This really is known as by Android every time the consumer clicks the menu. This really is helpful for enabling, crippling, adding or getting rid of menu products in line with the current condition of the application.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>