UNIT-5
Building Menus and Storing Data
Creating Interactive Menus and ActionBars
Using Databases
Creating Interactive Menus and ActionBars
In almost all applications we encounter menus that display options in the form of menu items. Choosing a menu item results in the initiation of the desired task. Menus are therefore the
preferred way of indicating the possible actions in an application.
The ActionBar is a widget that replaces the title bar at the top of an Activity displaying navigation and important functionality of an application. It provides a consistent UI of an application. It also displays the key actions that are commonly used in an application and that are constantly visible on the screen. ActionBar is also commonly used to provide a quick link to an applications home.
MENUS AND THEIR TYPES Android SDK supports three types of menus: Options, Submenu, and Context, as detailed in the following list:
Options Menu Also known as the Activity menu, this menu is displayed when a MENU button is clicked. In an Options Menu, the menu items are displayed in the form of text, check box, or radio buttons. It can display shortcut keys but does not display icons. In older Android API levels (Levels 10 and below), there were two types of Options Menus:
Icon Menu The Icon Menu appears when the user presses the MENU button on the device. The Icon Menu shows the first six menu items of the menu in the form of large, finger-friendly buttons arranged in a grid at the bottom of the screen. The menu items in the Icon Menu can display icons as well as text. The Icon Menu does not display check boxes, radio buttons, or the shortcut keys for menu items. It is not mandatory to have icons for Icon Menu items; text will also do for the Icon Menu.
Expanded Menu If the menu has more than six menu items, the first five items are displayed as an Icon Menu and the sixth option appears as a More button. Clicking the More button displays the Expanded Menu that shows the scrollable list of the rest of the menu items that could not be accommodated in the Icon Menu. The Expanded Menu can display menu items in the form of text, check boxes, or radio buttons. Also, it can display shortcut keys but does not display icons. Pressing the Back button from the Expanded Menu takes you back to the Activity the menu was launched from.
Note
The Icon Menu and Expanded Menu are not supported in Android API levels 11 and higher.
Submenu Submenu refers to the menu that displays more detailed or specific menu options when a menu item is selected. A submenu is displayed as a floating window showing all of its menu options. The name of the submenu is shown in the header bar, and each menu option is displayed with its full text, check box, radio button (if any), and shortcut key. Icons are not displayed in the submenu options. We cannot add a submenu to any menu option of a submenu as Android does not support nested submenus. Pressing the Back button closes the floating window without navigating back to the Expanded or Icon menus.
Context Menu The Context Menu is displayed when we tap-and-hold on the concerned View or when a user holds the middle D-pad button or presses the trackball. Context Menus support submenus, check boxes, radio buttons, and shortcuts, but we cannot display icons in a Context Menu. In the Context Menus header bar, we can display a title and icon.
CREATING MENUS THROUGH XMLBesides through Java code, in Android, menus can be created through Resources too. We can define a menu in the form of an XML file, which is then loaded by the Android SDK. As usual, Android generates resource IDs for each of the loaded menu items. The XML file for the menu has to be kept in a specific, designated folder that does not exist by default; we need to create it manually.
To understand the procedure of creating menus practically, lets create a new Android application called MenuApp. We see that a folder called menu already exists in our res/ directory. The menu folder also contains a file activity_menu_app.xml by default. We learn to create all three types of menus: Options Menu, Submenu, and Context Menu. Lets begin with the Options Menu.
Creating an Options MenuAn Options Menu is the menu displayed when the MENU button on the device is clicked. It shows menu items in the form of text, check boxes, and radio buttons. Also, the shortcut keys can be assigned to the menu items. An Options Menu is defined through an XML file in the res/menu folder of the application. That is, the menu items of the Options Menu are defined in that XML file.
The activity_menu_app.xml file, which is automatically created for us, contains the following data:
android:title="@string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
We can see that the root node of our menu in the XML file is
. The default menu item with the ID menu_settings shows the text that is defined in the strings.xml file. Lets define certain nodes to display certain menu items. The code written in the activity_menu_app.xml file is as shown in Listing 7.1 .Note
Each node represents a menu item in the menu file.
Listing 7.1. The Menu Items Defined in the File activity_menu_app.xml android:title="Create Database"
android:icon="@drawable/ic_launcher" />
android:title="Insert Rows"
android:icon="@drawable/ic_launcher" />
android:title="List Rows" />
android:title="Search"
android:icon="@drawable/ic_launcher"/>
android:title="Delete" />
android:title="Update"
android:icon="@drawable/ic_launcher" />
We can see that we have defined six menu items in the preceding menu, and a unique ID is assigned to each of them. The android:title and android:icon attribute defines the text and icon for the menu item. The menu items defined in our menu file are Create Database, Insert Rows, List Rows, Search, Delete, and Update. The six menu items are assigned the IDs as create_datab, insert_rows, list_rows, search_row, delete_row, and update_row, respectively. In the application, we want to display a message that asks the user to select the MENU button on the device or emulator for displaying the Icon Menu. We use the TextView control to display messages. To define TextView, we write the code as shown in Listing 7.2 in the layout file activity_menu_app.xml.
Listing 7.2. The Layout File activity_menu_app.xml on Adding the TextView Control
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/selectedopt" />
We added a TextView with the ID selectedopt to the default layout LinearLayout that we use to display desired text messages to the user. The TextView also is used to inform which menu item is selected by the user.
To inflate or merge the menu that we defined in the mymenu.xml file in our application and also to inform which menu item is selected by the user, we write the Java code as shown in Listing 7.3 in our Activity file MenuAppActivity.java.
Listing 7.3. Code Written in the Java Activity File MenuAppActivity.java package com.androidunleashed.menuapp;
import android.app.Activity; import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuInflater; import android.widget.TextView;
public class MenuAppActivity extends Activity { private TextView selectedOpt;
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_app);
selectedOpt=(TextView)findViewById(R.id.selectedopt); selectedOpt.setText("Please select MENU button to display menu");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.activity_menu_app, menu); return true;