Options Menu

Option Menus are the primary menus of android. They can be used for settings, search, delete item etc. These Option Menus can be as text or text with image.

Here, we are inflating the menu by calling the inflate() method of MenuInflater class. To perform event handling on menu items, you need to override onOptionsItemSelected() method of Activity class.

Let's create first res/menu/main.xml

res/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/mnuSpeak"
        android:icon="@android:drawable/ic_btn_speak_now"
        android:title="Speak"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/mnuCall"
        android:icon="@android:drawable/ic_menu_call"
        android:title="Call Someone"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/mnuDone"
        android:title="All Ok"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/mnuDelete"
        android:icon="@android:drawable/ic_delete"
        android:title="Delete"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_settings"
        android:title="Settings"
        app:showAsAction="never" />
</menu>

Create MainActivity.java
package com.theitbulls.optionmenuex;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.mnuCall:
                Toast.makeText(this, "Clicked on Call Menu", Toast.LENGTH_SHORT).show();
            case R.id.mnuDelete:
                Toast.makeText(this, "Clicked on Delete Menu", Toast.LENGTH_SHORT).show();
            case R.id.mnuDone:
                Toast.makeText(this, "Clicked on Done Menu", Toast.LENGTH_SHORT).show();
            case R.id.mnuSpeak:
                Toast.makeText(this, "Clicked on Speak Menu", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}

For Custom Menu you must read ActionBar