Creating Options Menu for android

There is another common task in android applications is creating the options menu
Menus are an important part of an activity’s user interface, which provide users a familiar way to perform actions. Android offers a simple framework for you to add standard menus to your application.

You can find more information on menus on android development website at following url

http://developer.android.com/guide/topics/ui/menus.html

So now we will create two simple menus one is settings and another one is about

First thing to move ahead with is to create a menu xml file which will look like as follows

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/settings"
        android:icon="@drawable/settings"
        android:title="Settings"
         />
    <item android:id="@+id/about"
        android:title="About"
        android:icon="@drawable/about"
         /> 

</menu>

Create a new folder in the res directory and save it as menu.xml

After that create a new menu in java file as follows

 // options menu
	  @Override
	  public boolean onCreateOptionsMenu(Menu menu) {
	      MenuInflater inflater = getMenuInflater();
	      inflater.inflate(R.menu.menu, menu);
	      return true;
	  }

So when click on menu button you will get the two menus created mainly settings and about

Now lets have the action that will execute upon clicking the menu

 public boolean onOptionsItemSelected(MenuItem item) {
		  switch (item.getItemId()) 
		  {
		      case R.id.settings:
		          ShowSettingsMenu();// function to call on click on settings menu
		      break;
		      case R.id.about:
		          ShowAboutPopup();// function to call on click on about menu
		      break;

		  }
		  return false;
		  }

when you click on the menu you can start a new activity or a dialog box or any other action

 

Isnt it pretty simple one..

Advertisements

What you think ? Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s