ShareThis

Monday, November 21, 2011

[ANDROID] How to create an option menu - SDK


Today we learn how you can create an option menu for your application.
Lets start with an empty android project. The package name will becom.droidnova.android.howto.optionmenu and the activity will have the nameSimpleOptionMenu.
Our activity should now look very familiar to us:
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.droidnova.android.howto.optionmenu;
 
import android.app.Activity;
import android.os.Bundle;
 
public class SimpleOptionMenu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

First we have to make a new folder in our res/ directory named menu. In this new directory we will create a new xml file named menu.xml.
1
2
3
4
5
6
7
8
9
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/icon"
        android:icon="@drawable/icon" />
    <item android:id="@+id/text"
        android:title="Text" />
    <item android:id="@+id/icontext"
        android:title="Icon and text"
        android:icon="@drawable/icon" />
</menu>
The content of the xml file should be very self explaining. We have an id for each item, so we have a reference for it. The title attribute, if defined, is nothing more than the text you see in the option menu. The same with the icon attribute which references to an icon, in our case the default icon.
Now we have to modify our SimpleOptionMenu activity class. First we have to override the method onCreateOptionsMenu().
1
2
3
4
5
6
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
On line 2 you see the parameter menu. In this menu we will inflate our own menu defined by our menu.xml.
On line 3 we get the MenuInflater from the Activity class. We need this MenuInflater object to inflate, or merge, our own menu in the menu given as a parameter on line 2.
Line 4 inflate the given menu with our own menu. Line 5 is just our return type.
Some explanation: I talked a lot of menus so far, but we just have two different menus. As far as I understand, the menu given as a parameter always exist inside an activity, we simply don’t see it if we press the menu button. The reason should be clear: there is no standard menu defined so the menu has nothing to display. We change that by overriding this method and inflating our own menu.
Right now we can start our application and we will see our 3 option items in our menu when we press the menu button. As long as nothing happens when we press one item, the menu is senseless.
To implement a reaction of our menu, we should override another method named onOptionsItemSelected().
1
2
3
4
5
6
7
8
9
10
11
12
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icon:     Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
                            break;
        case R.id.text:     Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
                            break;
        case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show();
                            break;
    }
    return true;
}
On line 2 you see the parameter which works the same way we know from onTouchEvent(). The parameter item represents the item we pressed in our menu.
A simple switch/case construction with the item.getItemId() execute the code we want. In our case it is a simple Toast which will display a text that describes which item you touched.

0 comments:

Post a Comment