Creating Android Menus

// By:Team // No Comment

Email this to someonePrint this pageShare on Google+23Share on Facebook38Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest1Share on StumbleUpon20

Android Menus can be defined as bellow three types:

  1. Android Option Menu
  2. Android Context Menu
  3. Android Popup Menu

1-Android Option Menu

Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc.

Here, we are going to see two examples of option menus. First, the simple option menus and second, options menus with images.

Here, we are inflating the menu by calling the inflate() method of MenuInflater class. To perform event handling on menu items, you need to override onOptionsItemSelected() method of Activity

Let’s see how to create menu in android. Let’s see the simple option menu example that contains three menu items.

activity_main.xml

We have only one textview in this file.

File: activity_main.xml

 

menu_main.xml

It contains three items as show below. It is created automatically inside the res/menu directory.

File: menu_main.xml

Activity class

This class displays the content of menu.xml file and performs event handling on clicking the menu items.

File: MainActivity.java

 


Output:

Output after clicking on the menu button.


Creating Android Menus 1

Option Menu with Icon

You need to have icon images inside the res/drawable directory. The android:icon element is used to display the icon on the option menu. You can write the string information in the strings.xml file. But we have written it inside the menu_main.xml file.

File: menu_main.xml

 

 

2-Android Context Menu

Android context menu appears when user press long click on the element. It is also known as floating menu.

It doesn’t support item shortcuts and icons.Let’s see the simple example of context menu in android.

activity_main.xml

Drag one listview from the pallete, now the xml file will look like this:

File: activity_main.xml

 


Activity class

Let’s write the code to display the context menu on press of the listview.

File: MainActivity.java

 


Output:

Output after long press on the listview.

Creating Android Menus 2

 

3-Android Popup Menu

Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.

The android.widget.PopupMenu is the direct subclass of java.lang.Object class.Let’s see how to create popup menu in android.

activity_main.xml

It contains only one button.

File: activity_main.xml

 

popup_menu.xml

It contains three items as show below. It is created inside the res/menu directory.

File: poupup_menu.xml

 


Activity class

It displays the popup menu on button click.

File: MainActivity.java

 


Output:

Creating Android Menus 3

Email this to someonePrint this pageShare on Google+23Share on Facebook38Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest1Share on StumbleUpon20

About Team

Browse Archived Articles by Team

Related

Sorry. There are no related articles at this time.