Monday, June 18, 2012

How To Implement Sum Menu Into Your Application

The project describes about how to implement Sub menu into your application. A submenu is a menu that the user can open by selecting an item in another menu. You can add a submenu to any menu (except a submenu). 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.).
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project MySubMenu
2.) Create and open the res/menu/menu.xml file and insert the following:
<menu xmlns:android="">
        <item android:id="@+id/Menu1" android:orderInCategory="1"
                android:title="Add" />
        <item android:id="@+id/submenu" android:title="Send"
                        <item android:id="@+id/submenu1" android:title="Via BlueTooth" />
                        <item android:id="@+id/submenu2" android:title="Via SMS"/>
        <item android:id="@+id/Menu3" android:orderInCategory="3"
                android:title="Copy" />
        <item android:id="@+id/Menu4" android:title="Delete"
                android:orderInCategory="4" />
3.) Make sure you write following method in MySubMenu class:
public boolean onCreateOptionsMenu(Menu menu)
        new MenuInflater(getApplication()).inflate(, menu);
4.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MySubMenu Enter following information:
Project name: MySubMenu
Build Target: Android 1.6
Application name: MySubMenu
Package name: org.example.MySubMenu
Create Activity: MySubMenu
On Clicking Finish MySubMenu code structure is generated with the necessary Android Packages being imported along with Following code must be added in MySubMenu class to get the menu to work.
public void onCreate(Bundle savedInstanceState)
public boolean onOptionsItemSelected(MenuItem item)
        switch (item.getItemId())
                        Toast.makeText(this"Add", Toast.LENGTH_SHORT).show();
                        Toast.makeText(this"Send", Toast.LENGTH_SHORT).show();
                        Toast.makeText(this"Copy", Toast.LENGTH_SHORT).show();
                        Toast.makeText(this"Delete", Toast.LENGTH_SHORT).show();
Output –The final output:

No comments:

Post a Comment