DAY15
ANDROID
MENU
MENU:
Menus are a common user interface component in many types of applications. To provide a familiar and consistent user experience, you should use the
Menu
APIs to present user actions and other options in your activities.
Beginning with Android 3.0 (API level 11), Android-powered devices are no longer required to provide a dedicated Menu button. With this change, Android apps should migrate away from a dependence on the traditional 6-item menu panel and instead provide an app bar to present common user actions.
Although the design and user experience for some menu items have changed, the semantics to define a set of actions and options is still based on the
Menu
APIs. Defining a Menu in XML
For all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity's code, you should define a menu and all its items in an XML menu resource. You can then inflate the menu resource (load it as a
Menu
object) in your activity or fragment.
Using a menu resource is a good practice for a few reasons:
- It's easier to visualize the menu structure in XML.
- It separates the content for the menu from your application's behavioral code.
- It allows you to create alternative menu configurations for different platform versions, screen sizes, and other configurations by leveraging the app resources framework.
To define the menu, create an XML file inside your project's
res/menu/
directory and build the menu with the following elements:<menu>
- Defines a
Menu
, which is a container for menu items. A<menu>
element must be the root node for the file and can hold one or more<item>
and<group>
elements. <item>
- Creates a
MenuItem
, which represents a single item in a menu. This element may contain a nested<menu>
element in order to create a submenu. <group>
- An optional, invisible container for
<item>
elements. It allows you to categorize menu items so they share properties such as active state and visibility. For more information, see the section about Creating Menu Groups.
Here's an example menu named
game_menu.xml
:<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@+id/new_game"android:icon="@drawable/ic_new_game"android:title="@string/new_game"android:showAsAction="ifRoom"/><item android:id="@+id/help"android:icon="@drawable/ic_help"android:title="@string/help" /></menu>
The
<item>
element supports several attributes you can use to define an item's appearance and behavior. The items in the above menu include the following attributes:android:id
- A resource ID that's unique to the item, which allows the application to recognize the item when the user selects it.
android:icon
- A reference to a drawable to use as the item's icon.
android:title
- A reference to a string to use as the item's title.
android:showAsAction
- Specifies when and how this item should appear as an action item in the app bar.
These are the most important attributes you should use, but there are many more available. For information about all the supported attributes, see the Menu Resource document.
You can add a submenu to an item in any menu by adding a
<menu>
element as the child of an <item>
. Submenus are useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.). For example:<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@+id/file"android:title="@string/file" ><!-- "file" submenu --><menu><item android:id="@+id/create_new"android:title="@string/create_new" /><item android:id="@+id/open"android:title="@string/open" /></menu></item></menu>
To use the menu in your activity, you need to inflate the menu resource (convert the XML resource into a programmable object) using
MenuInflater.inflate()
.
CREATING OPTION MENU
To specify the options menu for an activity, override
onCreateOptionsMenu()
(fragments provide their own onCreateOptionsMenu()
callback). In this method, you can inflate your menu resource (defined in XML) into the Menu
provided in the callback. For example:@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater =getMenuInflater()
;inflater.inflate(R.menu.game_menu, menu);return true;}
You can also add menu items using
add()
and retrieve items with findItem()
to revise their properties with MenuItem
APIs.
If you've developed your application for Android 2.3.x and lower, the system calls
onCreateOptionsMenu()
to create the options menu when the user opens the menu for the first time. If you've developed for Android 3.0 and higher, the system calls onCreateOptionsMenu()
when starting the activity, in order to show items to the app bar.Handling click events
When the user selects an item from the options menu (including action items in the app bar), the system calls your activity's
onOptionsItemSelected()
method. This method passes the MenuItem
selected. You can identify the item by calling getItemId()
, which returns the unique ID for the menu item (defined by the android:id
attribute in the menu resource or with an integer given to the add()
method). You can match this ID against known menu items to perform the appropriate action. For example:@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle item selectionswitch (item.getItemId()) {case R.id.new_game:newGame();return true;case R.id.help:showHelp();return true;default:return super.onOptionsItemSelected(item);}}
When you successfully handle a menu item, return
true
. If you don't handle the menu item, you should call the superclass implementation of onOptionsItemSelected()
(the default implementation returns false).
If your activity includes fragments, the system first calls
onOptionsItemSelected()
for the activity then for each fragment (in the order each fragment was added) until one returns true
or all fragments have been called.Creating Contextual Menus

Figure : Screenshots of a context menu
A contextual menu offers actions that affect a specific item or context frame in the UI. You can provide a context menu for any view, but they are most often used for items in a
ListView
, GridView
, or other view collections in which the user can perform direct actions on each item.
To provide a floating context menu:
- Register the
View
to which the context menu should be associated by callingregisterForContextMenu()
and pass it theView
.If your activity uses aListView
orGridView
and you want each item to provide the same context menu, register all items for a context menu by passing theListView
orGridView
toregisterForContextMenu()
. - Implement the
onCreateContextMenu()
method in yourActivity
orFragment
.When the registered view receives a long-click event, the system calls youronCreateContextMenu()
method. This is where you define the menu items, usually by inflating a menu resource. For example:@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {super.onCreateContextMenu(menu, v, menuInfo);MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.context_menu, menu);}MenuInflater
allows you to inflate the context menu from a menu resource. The callback method parameters include theView
that the user selected and aContextMenu.ContextMenuInfo
object that provides additional information about the item selected. If your activity has several views that each provide a different context menu, you might use these parameters to determine which context menu to inflate. - Implement
onContextItemSelected()
.When the user selects a menu item, the system calls this method so you can perform the appropriate action. For example:@Overridepublic boolean onContextItemSelected(MenuItem item) {AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();switch (item.getItemId()) {case R.id.edit:editNote(info.id);return true;case R.id.delete:deleteNote(info.id);return true;default:return super.onContextItemSelected(item);}}ThegetItemId()
method queries the ID for the selected menu item, which you should assign to each menu item in XML using theandroid:id
attribute, as shown in the section about :Defining a Menu in XML.
true
. If you don't handle the menu item, you should pass the menu item to the superclass implementation. If your activity includes fragments, the activity receives this callback first. By calling the superclass when unhandled, the system passes the event to the respective callback method in each fragment, one at a time (in the order each fragment was added) until true
or false
is returned. (The default implementation for Activity
and android.app.Fragment
return false
, so you should always call the superclass when unhandled.)
LIST VIEW:
An adapter actually bridges between UI components and the data source that fill data into UI Component. Adapter holds the data and send the data to adapter view, the view can takes the data from adapter view and shows the data on different views like as spinner, list view, grid view etc.
The ListView and GridView are subclasses of AdapterView and they can be populated by binding them to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.
Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView ( i.e. ListView or GridView). The common adapters are ArrayAdapter,Base Adapter,CursorAdapter, SimpleCursorAdapter,SpinnerAdapter and WrapperListAdapter. We will see separate examples for both the adapters.
ARRAY_ADAPTER
You can use this adapter when your data source is an array. By default, ArrayAdapter creates a view for each array item by calling toString() on each item and placing the contents in a TextView. Consider you have an array of strings you want to display in a ListView, initialize a new ArrayAdapter using a constructor to specify the layout for each string and the string array −
ArrayAdapter adapter=new ArrayAdapter<String>(this,R.layout.ListView,StringView);
Once you have array adapter created, then simply call setAdapter() on your ListView object as follows −
ListView listview=(ListView)findviewById(R.id.listview);
listView.setAdapter(adapter);
Comments
Post a Comment