Lesson 13. Creating a Simple Menu

Lesson 13. Creating a Simple Menu


In this lesson we:

– create menu items

What is a menu, I think, there is no point in telling. It is displayed when the Menu button is pressed. Let’s create our own.

11/12/2017 Menu buttons are usually gone on modern devices. The menu moved to ActionBar.

You can read more about this in Lesson 107. Some screenshots are outdated there, but the code and general content are relevant.

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. An object of type is fed to the input MenuTo which we will add our items.

Add this method to Activity:

    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);
    }

Menu items are added using the add method. The method input is fed text menu items. Add 4 points.

The onCreateOptionsMenu method must return a result of type boolean. True – show menu, False – DO NOT show. That is, it would be possible to test any condition in the short run, and the results of this test would not show the menu passing False. While we do not need it, so we instruct this choice of superclass method, it returns True by default.

Let’s save everything, run the program and click the menu button on the emulator.

4 menu items appear. Clicking on them does not lead to anything because the handler is not implemented. The handler is Activity, and the method is called onOptionsItemSelected. A menu item is pressed to the input that has been clicked – MenuItem. You can determine which menu has been clicked using the getTitle method. Let’s display a pop-up message with the text of the menu item pressed. The output of the method must be returned boolean. And again we provide this 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);
    }
    
}

Defining a menu item by text is not the best option. Next we will do it on ID. But it takes a little different to create a menu.

In the next lesson:

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




Discuss in the forum [103 replies]

Leave a Comment