Android program to implement Options Menu

Android program to implement Options Menu

Android program to implement Options Menu. Options Menu contains menu items for an activity. Options Menu must contain actions which have a global impact on the app, like delete, search,settings and other actions. For example, if you have opened the Google Chrome app on your mobile then you can see three dots to the top right, that is nothing but the options menu(See image), one more example is of WhatsApp even they have the options menu to the top right.

Now coming to the program we will create an options menu having different products and on click on those products, we will display Toast messages. To implement the options menu we first need override the implementation of onCreateOptionsMenu and inside this, we need to call the inflate() method of the MenuInflator class. Create a list of items in options_menu.xml file which you want to show in the options menu, this is done by using <item></item>. Now, in order to perform an action on click of these items(i.e. nothing but event handling), we need to override onOptionsItemSelected() method of Activity class.

Steps to create the application:- 

  • Open Android Studio and create a new Android application and name it as “OptionsMenu” and company domain as codedost so your package will be automatically set.
  • Open an Empty Activity and name it as MainActivity.
  • You will also need to create a menu directory and then create an options_menu.xml file in that directory. To achieve this first right click on res, goto new, goto android resource directory, in directory name write menu, select the resource type as menu and source set as main, click OK. Now you will have your menu directory created. Then to create the options_menu.xml file, right click on menu which you just created, click on new, you should get an option as menu resource file click on that, set filename as options_menu, source set as main and your directory name as menu, click OK. Now you have successfully created your options_menu.xml file.
  • Copy the contents of res/layout/activity_main.xml and res/menu/options_menu.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

XML File(res/layout/activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="codedost.optionsmenu.MainActivity">

</RelativeLayout>

XML File(res/menu/option_menu.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/mob"
                android:title="Mobile"
                app:showAsAction="never"/>
        <item
            android:id="@+id/lp"
            android:title="Laptop"
            app:showAsAction="never"/>
        <item
            android:id="@+id/comp"
            android:title="Computer"
            app:showAsAction="never"/>
</menu>

MainActivity.java

package codedost.optionsmenu;

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
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.option_menu,menu);
        //setTitle("SELECT A PRODUCT");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId())
        {
            case R.id.mob:
                Toast.makeText(this,"You have selected Mobile",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.lp:
                Toast.makeText(this,"You have selected Laptop",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.comp:
                Toast.makeText(this,"You have selected Computer",Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

Output

Share Me!