Tuesday, January 15, 2013

Android beginner tutorial: Part 52 - Submenus

In this tutorial we are going to learn about submenus.

Submenus can be added to both Context and Options menus. They can't, however, be used in other submenus.

Start by going to MainActivity.java class and declaring 6 id variables:

public static final int IDM_ONE = 101;
public static final int IDM_TWO = 102;
public static final int IDM_THREE = 103;
public static final int IDM_FOUR = 104;
public static final int IDM_FIVE = 105;
public static final int IDM_SIX = 106;

Thr onCreate() function is left untouched from its default state:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

We'll create an Options menu and add 2 items to it, both of which are submenus. Keep in mind that it is possible to use both submenus and regular items in a menu.

Create an onCreateOptionsMenu() function, inside of it add 2 SubMenu items. Set their values to menu.addSubMenu(), pass submenu item name as the parameter. It will be displayed as the label of the Options menu item, as well as the title of the submenu list.

Then add() items to the submenu. Add 2 submenus this way with 3 items in each of them:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenu1 = menu.addSubMenu("Submenu 1");
subMenu1.add(Menu.NONE, IDM_ONE, Menu.NONE, "Item one");
subMenu1.add(Menu.NONE, IDM_TWO, Menu.NONE, "Item two");
subMenu1.add(Menu.NONE, IDM_THREE, Menu.NONE, "Item three");

SubMenu subMenu2 = menu.addSubMenu("Submenu 2");
subMenu2.add(Menu.NONE, IDM_FOUR, Menu.NONE, "Item four");
subMenu2.add(Menu.NONE, IDM_FIVE, Menu.NONE, "Item five");
subMenu2.add(Menu.NONE, IDM_SIX, Menu.NONE, "Item six");

return super.onCreateOptionsMenu(menu);
}

Add an item select listener as usual:

@Override
public boolean onOptionsItemSelected(MenuItem item){
CharSequence message;
switch(item.getItemId()){
case IDM_ONE:
message = "First item selected";
break;
case IDM_TWO:
message = "Second item selected";
break;
case IDM_THREE:
message = "Third item selected";
break;
case IDM_FOUR:
message = "Fourth item selected";
break;
case IDM_FIVE:
message = "Fifth item selected";
break;
case IDM_SIX:
message = "Sixth item selected";
break;
default:
return super.onContextItemSelected(item);
}
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.show();
return true;
}

And you are done!

Here is the full code:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class MainActivity extends Activity{

public static final int IDM_ONE = 101;
public static final int IDM_TWO = 102;
public static final int IDM_THREE = 103;
public static final int IDM_FOUR = 104;
public static final int IDM_FIVE = 105;
public static final int IDM_SIX = 106;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenu1 = menu.addSubMenu("Submenu 1");
subMenu1.add(Menu.NONE, IDM_ONE, Menu.NONE, "Item one");
subMenu1.add(Menu.NONE, IDM_TWO, Menu.NONE, "Item two");
subMenu1.add(Menu.NONE, IDM_THREE, Menu.NONE, "Item three");

SubMenu subMenu2 = menu.addSubMenu("Submenu 2");
subMenu2.add(Menu.NONE, IDM_FOUR, Menu.NONE, "Item four");
subMenu2.add(Menu.NONE, IDM_FIVE, Menu.NONE, "Item five");
subMenu2.add(Menu.NONE, IDM_SIX, Menu.NONE, "Item six");

return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
CharSequence message;
switch(item.getItemId()){
case IDM_ONE:
message = "First item selected";
break;
case IDM_TWO:
message = "Second item selected";
break;
case IDM_THREE:
message = "Third item selected";
break;
case IDM_FOUR:
message = "Fourth item selected";
break;
case IDM_FIVE:
message = "Fifth item selected";
break;
case IDM_SIX:
message = "Sixth item selected";
break;
default:
return super.onContextItemSelected(item);
}
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.show();
return true;
}

}

Thanks for reading!

No comments:

Post a Comment