Android – Using Menus with Views

Using Menus with Views

Android – Using Menus with Views

Menus are useful for showing additional options that are not directly visible on the main user interface (UI) of an application. There are two main kinds of menus in Android:
➤ Options menu—This menu shows information related to the current activity. In Android, you operate the options menu by pressing the Menu button.
➤ Context menu—This menu shows information related to a particular view on an . In Android, you tap and hold a context menu to operate it.

Creating the Helper Methods

Before you go ahead and generate your options and context menus, you need to create two helper methods. One creates a list of items to display inside a menu, whereas the other handles the event that is fired when the user choose an item inside the menu.

Creating the Menu Helper Methods

Step 1. First of all, Open your Android Studio and create new empty project and names it Menus.

Step 2. Now open the MainActivity.java file, add the following below statements:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
private void createMenu(Menu menu) {
 MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
 {
 mnu1.setAlphabeticShortcut('a');
 }
 MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
 {
 mnu2.setAlphabeticShortcut('b');
 }
 MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
 {
 mnu3.setAlphabeticShortcut('c');
 }
 MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
 {
 mnu4.setAlphabeticShortcut('d');
 }
 menu.add(0, 4, 4, "Item 5");
 menu.add(0, 5, 5, "Item 6");
 menu.add(0, 6, 6, "Item 7");
 }
 private boolean MenuChoice(MenuItem item) {
 switch (item.getItemId()) {
 case 0:
 Toast.makeText(this, "You clicked on Item 1",
 Toast.LENGTH_LONG).show();
 return true;
 case 1:
 Toast.makeText(this, "You clicked on Item 2",
 Toast.LENGTH_LONG).show();
 return true;
 case 2:
 Toast.makeText(this, "You clicked on Item 3",
 Toast.LENGTH_LONG).show();
 return true;
 case 3:
 Toast.makeText(this, "You clicked on Item 4",
 Toast.LENGTH_LONG).show();
 return true;
 case 4:
 Toast.makeText(this, "You clicked on Item 5",
 Toast.LENGTH_LONG).show();
 return true;
 case 5:
 Toast.makeText(this, "You clicked on Item 6",
 Toast.LENGTH_LONG).show();
 return true;
 case 6:
 Toast.makeText(this, "You clicked on Item 7",
 Toast.LENGTH_LONG).show();
 return true;
 }
 return false;
 }
}
How It Works

The preceding example creates two methods:
➤ createMenu()
➤ menuChoice()
The createMenu() method adds a series of menu items to a Menu argument.
To add a menu item to the menu, you generate an instance of the MenuItem class and use the Menu object’s
add() method:

MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
 {
 mnu1.setAlphabeticShortcut('a');
 mnu1.setIcon(R.mipmap.ic_launcher);
 }

The four arguments of the add() method are
➤ groupId—The group identifier of which the menu item should be a part. Use 0 if an item is not in a group.
➤ itemId—A unique item ID.
➤ order—The order in which the item should be showed.
➤ title—The text to show for the menu item.
You may use the setAlphabeticShortcut() method to assign a shortcut key to the menu item so that users can add an item by pressing a key on the keyboard. The setIcon() procedure sets an image to be showed on the menu item.
The menuChoice() method takes a MenuItem argument and checks its ID to determine the menu item that is selected. It then shows a Toast message to let the user know which menu item was selected.

Options Menu

You are now ready to modify the application to display the options menu when the user presses the Menu key on the Android device.

Displaying an Options Menu

Step 1. Using the same project created in the previous section, add the following statements to the MainActivity.java file:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 super.onCreateOptionsMenu(menu);
 createMenu(menu);
 return true;
 }
 @Override
 public boolean onOptionsItemSelected(MenuItem item)
 {
 return menuChoice(item);
 }
 private void createMenu(Menu menu) {
 MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
 {
 mnu1.setAlphabeticShortcut('a');
 }
 MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
 {
 mnu2.setAlphabeticShortcut('b');
 }
 MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
 {
 mnu3.setAlphabeticShortcut('c');
 }
 MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
 {
 mnu4.setAlphabeticShortcut('d');
 }
 menu.add(0, 4, 4, "Item 5");
 menu.add(0, 5, 5, "Item 6");
 menu.add(0, 6, 6, "Item 7");
 }
 private boolean menuChoice(MenuItem item) {
 switch (item.getItemId()) {
 case 0:
 Toast.makeText(this, "You clicked on Item 1",
 Toast.LENGTH_LONG).show();
return true;
 case 1:
 Toast.makeText(this, "You clicked on Item 2",
 Toast.LENGTH_LONG).show();
 return true;
 case 2:
 Toast.makeText(this, "You clicked on Item 3",
 Toast.LENGTH_LONG).show();
 return true;
 case 3:
 Toast.makeText(this, "You clicked on Item 4",
 Toast.LENGTH_LONG).show();
 return true;
 case 4:
 Toast.makeText(this, "You clicked on Item 5",
 Toast.LENGTH_LONG).show();
 return true;
 case 5:
 Toast.makeText(this, "You clicked on Item 6",
 Toast.LENGTH_LONG).show();
 return true;
 case 6:
 Toast.makeText(this, "You clicked on Item 7",
 Toast.LENGTH_LONG).show();
 return true;
 }
 return false;
 }
}

Press Shift+F9 to debug the application on the Android emulator. The options menu that shows when you click the Menu button. To chose a menu item, either click an individual item or use its shortcut key (A to D, and applicable only to the first four items).

How It Works

To show the options menu for your activity, you need to implement two methods in your activity:
➤ onCreateOptionsMenu()
➤ onOptionsItemSelected()
The onCreateOptionsMenu() method is called when the Menu button is pushed. In this case, you call the createMenu() helper method to show the options menu.
When a menu item is chose, the onOptionsItemSelected() method is called. In this case, you call the menuChoice() method to show the menu item selected (and perform whatever action is appropriate).

Using Menus with Views