Download Code

Android Studio Menu Example

In android studio 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,Android Studio Menu,Android Studio Menu Example,Menu Example,Menu,

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 now.

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.

Create android studio menu example application project.

After creating android application project.You will have to create menu directory in android project under res directory.Then right click on menu directory New > Menu Resource File > 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 items in main.xml.

main.xml

Android Studio Menu,Android Studio Menu Example,Menu Example,Menu,

Before dealing with MainActivity.java class let me tell you that you are now dealing with simple menu.

Menu Inflater

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

After that go to your MainActivity.java class and do the following steps.

  • Implement onCreate() method if it is not there.
  • Implement onCreateOptionsMenu() method and inflat menu using xml file.

MainActivity.java

package com.example.anonymous.studiomenu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends AppCompatActivity {

    @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;
    }
}

Run your android menu example application project.

Android Studio Menu,Android Studio Menu Example,Menu Example,Menu,

Now you display icon instead of text.Go to your res > menu > main.xml file.Add icon path and display attribute.

main.xml

Android Studio Menu,Android Studio Menu Example,Menu Example,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

ok as previously seen that you have used onCreateOptionsMenu() method.Just for the sake of loading menu.Now you apply click event on each item or action bar icon.

For that purpose implement onOptionsItemSelected() method.Then use switch and case statement to get id for each item apply case for that item.I have applied only just on Setting item which will display Toast.You can set any action like start an activity etc.
MainActivity.java

package com.example.anonymous.studiomenu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @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;
    }
    @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);
    }
}

Run your application project.

Android Studio Menu,Android Studio Menu Example,Menu Example,Menu,

that’s it for this android studio menu example.I hope you get the idea of menus and action bar menus.

Don’t forget to leave your feedback.

Thanks.