13. Creating a simple menu

Lecture



In this lesson we:

- create menu items

What is the menu, I think, it makes no sense to tell. It is displayed when you press the Menu button. Let's create our own.

Create a project:

Project name : P0131_MenuSimple
Build Target : Android 2.3.3
Application name : MenuSimple
Package name : ru.startandroid.develop.menusimple
Create Activity : MainActivity

Open MainActivity.java . The onCreateOptionsMenu method is responsible for creating the menu. At the entrance it is given an object of type Menu , in which we will add our own items. They are simply added using the add method. The input to the method is the text of the menu item. Add 4 points.

public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub

menu.add("menu1");
menu.add("menu2");
menu.add("menu3");
menu.add("menu4");

return super.onCreateOptionsMenu(menu);
}

The onCreateOptionsMenu method should return a boolean result. True - show menu, False - do not show. Those. It would be possible to check a condition, and based on the results of this check, do not show the menu by passing False. While we do not need it, so we entrust this choice to the method of the superclass, by default it returns True.

Save everything, run the application and press the menu button on the emulator.

  13. Creating a simple menu

There are 4 menu items. Clicking on them leads to nothing, because handler not implemented. The handler is an Activity, and the method is called onOptionsItemSelected. The menu item that is clicked - MenuItem is passed to the input. You can determine which menu was pressed by the getTitle method. Let's display a pop-up message with the text of the menu item being clicked. At the output of the method must be returned boolean . And we again provide this to the superclass.

public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}

Full code:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub

menu.add("menu1");
menu.add("menu2");
menu.add("menu3");
menu.add("menu4");

return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}

}

Determining the menu item pressed by text is not the best option. Next we will do it by ID . But for this you need a little differently to create a menu.

In the next lesson:

- create menu items with ID
- group and sort menu items


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Mobile Programming (Android IOs)

Terms: Mobile Programming (Android IOs)