Android Menu Example

In android menu example you will learn how to create menu in android application.Ok let me give you some introduction about Menus and Action Bar.

Menu

The most commonly used user interface component in android application to provide better user experience with multiple options in activities.Menus begin from android version 3.0 (API 11).There are three basic type of menus.

Options menu with action bar

The collection of multiple options on which you can perform actions like searching etc.

Action bar

Action bar is also used to provide access to actions on items.

This picture might illustrate better than words.

menus

Option menus open when you press menus button from device.Action bar is always on top why Option menus deprecated because most of devices don’t have menus button.

Context Menu and Contextual action Mode

Context is a type of menus which display as list of menu when you perform long click action on any element.
Contextual action mode is used to perform actions.

Popup Menus

Display as a list of items more detail.

ok let’s start go ahead and create android menu example application project.

menu,android menu,android menu example,menu example,android settings menu,andorid hidden menu,

After creating android application project.You will see menudirectory in android project under res directory.Open directory you will get main.xml.This xml is basically used to add items.This file contains some elements.

  • menu(parent element).
  • item(child element of menus).
  • groups(child element of menus to contains one or more items).

Next every item have some attributes.

  • id(id reference of an item).
  • icon(icon for item).
  • title(title of that particular item).
  • action(when and how item should appear to perform actions).

Now next put that some items in main.xml just like this.

menu,android menu,android menu example,menu example,android settings menu,andorid hidden menu,

First you will create basic without any icon.Next open your MainActivity.java class by default your activity might look like this.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Next right click on Activity go to Source > Override Implement/Methods inside activity find onCreateOptionsMenu method and click ok.First what is MenuInflater?

Menu Inflater

Basically used to instantiate menus XML file into menus object.More Detailed.

Now back to MainActivity.java class you have two options to do that step.

First using using public constructor with public method.Second Menus Inflater object getting public constructor and the using that object to call public method.

Using First Option your activity will look like this.

import android.support.v7.app.ActionBarActivity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menus) {
        // Inflate this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menus);
        return true;
    }
}

Using Second option your activity will look like this.

package androidcodec.menus;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menus) {
        // Inflate this adds items to the action bar if it is present.
        
    	MenuInflater me = getMenuInflater();
    	me.inflate(R.menu.main, menus);
        return true;
    }
}

Now Run you android menu example application project.Your output will look like this in Android Device.

menu,android menu,android menu example,menu example,android settings menu,andorid hidden menu,

Next you will add icon with item your xml and final output will look like this.

menu,android menu,android menu example,menu example,android settings menu,andorid hidden menu,

menu,android menu,android menu example,menu example,android settings menu,andorid hidden menu,

Now let’s talk about Action Bar.To display these items on action bar what you have to do next is to add one line in your main.xml => android:showAsAction=”ifRoom”. Now your main.xml and Output will look like this.

menu,android menu,android menu example,menu example,android settings menu,andorid hidden menu,

menu,android menu,android menu example,menu example,android settings menu,andorid hidden menu,

These are the action bar icon sizes.

  • 24×24 for mdpi
  • 36×36 for hdpi
  • 48×48 for xhdpi
  • 72×72 for xxhdpi
  • 96×96 for xxxhdpi

Next what you have to do.You will code for each item when user select any option.So for that right click on Activity and go to Source > Override Implement/Methods and select onOptionsItemSelected() method.Then use switch and case statement to get id for each item apply case for that item just like this.

package androidcodec.menus;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate; this adds items to the action bar if it is present.
        
    	MenuInflater me = getMenuInflater();
    	me.inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
        	Toast.makeText(getApplicationContext(), "Setting Clicked", Toast.LENGTH_SHORT).show();   // Toast might be not display this is for any other action
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

I have applied only just on Setting item which will display Toast.You can set any action like start an activity etc.

I hope this android menu example might help you alot to understand.

Please don’t forget to leave your feedback.

Thanks.