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="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/Menu1" android:orderInCategory="1"
                android:title="Add" />
        <item android:id="@+id/submenu" android:title="Send"
                android:orderInCategory="2">
                <menu>
                        <item android:id="@+id/submenu1" android:title="Via BlueTooth" />
                        <item android:id="@+id/submenu2" android:title="Via SMS"/>
                </menu>
        </item>
        <item android:id="@+id/Menu3" android:orderInCategory="3"
                android:title="Copy" />
        <item android:id="@+id/Menu4" android:title="Delete"
                android:orderInCategory="4" />
</menu>
3.) Make sure you write following method in MySubMenu class:
public boolean onCreateOptionsMenu(Menu menu)
{
        new MenuInflater(getApplication()).inflate(R.menu.menu, menu);
return(super.onPrepareOptionsMenu(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 MySubMenu.java. Following code must be added in MySubMenu class to get the menu to work.
public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
}
public boolean onOptionsItemSelected(MenuItem item)
{
        switch (item.getItemId())
{
                case R.id.Menu1:
                        Toast.makeText(this"Add", Toast.LENGTH_SHORT).show();
                        break;
                case R.id.submenu:
                        Toast.makeText(this"Send", Toast.LENGTH_SHORT).show();
                        break;
                case R.id.Menu3:
                        Toast.makeText(this"Copy", Toast.LENGTH_SHORT).show();
                        break;
                case R.id.Menu4:
                        Toast.makeText(this"Delete", Toast.LENGTH_SHORT).show();
                break;
        }
        return(super.onOptionsItemSelected(item));
}
Output –The final output:

No comments:

Post a Comment