Menu example 

Menus are very commonly used element of Android which has been used in many types of applications. There are three types of menus in Android.
  1. Options Menu.
  2. Context Menu.
  3. Popup Menu.

Options Menu:

Options menu is a primary menu of Android this menu is used in many applications. With the help of this menu the application will have a global impact in it like 'Settings', 'Logout', 'Search', 'Delete' etc. Menu will appear in the top of the action bar with three dots or it will appear in the default menu button of your android device. In Options menu there are two types again.
  1. Without icons that appear in the form of list.
  2. With icons which appears on the action bar horizontally.
The following steps will help us to make a Options Menu example.
  • We can create a new project or continue in existing one.
  • Under res directory create a new directory as menu.
  • Under menu directory create a new file "right click-->new-->Menu resource file" give a name for the file and hit OK.
  • In the menu file add items as following.
File: menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item
        android:id="@+id/search"
        android:title="Search" />

    <item
        android:id="@+id/settings"
        android:title="Settings" />

    <item
        android:id="@+id/logout"
        android:title="Logout" />

</menu>
  • Now go to MainActivity.java file and override two methods by pressing ctrl+o buttons. You will get the list of methods just select the following two methods from it.
  1. OnCreateOptionsMenu = this method is used for inflating a menu directory and menu main xml file.
  2. OnOptionsItemSelected = this method is used for on click event handling on the menu items.
File: MainActivity.java

package infinite.hss.com.menuexample;

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

    }

    // for inflating the menu_main.xml    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    // for on click handling    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:
                Toast.makeText(this, "Clicked on " + item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.settings:
                Toast.makeText(this, "Clicked on " + item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.logout:
                Toast.makeText(this, "Clicked on " + item.getTitle(), Toast.LENGTH_SHORT).show();
                break;

        }
        return super.onOptionsItemSelected(item);
    }
}
  • Run the app and enjoy the options menu.

  • With Icons:
To add icons to the options menu you just need to add two extra attributes in menu_main.xml file as following.

File: menu_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="Search"
        app:showAsAction="always" />

    <item
        android:id="@+id/logout"
        android:icon="@android:drawable/ic_lock_power_off"
        android:title="Logout"
        app:showAsAction="always" />

    <item
        android:id="@+id/settings"
        android:title="Settings" />

</menu>
  • Run the app and enjoy options menu with icons.

Comments