Option Menu-Android

by Murtaza FarhatYar | 22 Jan 2019

Menu is one of the most useful and important component/widget. As menu has many different types as their usages differ. In this article we are going to be familiar with option menu in android.

follow the following steps and points.

First step:

In beginning you have to create the menu directory as bellow:

creating_menu_directory

Second step:

Keep in mind that the directory name must be menu, nothing else:



menu_directory_name

Third step:

right click on menu that you have created and add the menu resource file as bellow.

Fourth step:

give a name for menu resource file.

Fifth step:

after creating menu resource file, write the following codes in option_menu_src file.

<?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">
    // list all the options using item
    <item
        android:id="@+id/shareId"
        android:title="Share"
        android:icon="@android:drawable/ic_menu_share"
        app:showAsAction="always"
        ></item>
    <item
        android:id="@+id/searchId"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        ></item>
    <item
        android:id="@+id/EditId"
        android:title="Edit"
        android:icon="@android:drawable/ic_menu_edit"
        app:showAsAction="always"
        ></item>
    <item
        android:id="@+id/copyId"
        android:title="Copy"
        ></item>
    <item
        android:id="@+id/deleteId"
        android:title="Delete"
        ></item>
</menu>
See also: Dynamic ListView – Android
Sixth step:

the last step is adding the related codes in MainActivity.java.

package com.example.zakia.testapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
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
    // this method must be overrided due to creation of menu when the activity is started
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        // gets two parameters: the first one is menu_resouce and the menu itself
        inflater.inflate(R.menu.option_menu_src, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    // for menu items hunddling, override this method
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.shareId) {
            Toast.makeText(this, "You Selected share", Toast.LENGTH_LONG).show();
        }
        if (item.getItemId() == R.id.searchId) {
            Toast.makeText(this, "You Selected Search", Toast.LENGTH_LONG).show();
        }
        if (item.getItemId() == R.id.EditId) {
            Toast.makeText(this, "You Selected Edit", Toast.LENGTH_LONG).show();
        }
        if (item.getItemId() == R.id.copyId) {
            Toast.makeText(this, "You Selected Copy", Toast.LENGTH_LONG).show();
        }
        if (item.getItemId() == R.id.deleteId) {
            Toast.makeText(this, "You Selected Delete", Toast.LENGTH_LONG).show();
        }
        return super.onOptionsItemSelected(item);
    }
}
The result is:

 

for more follow us on