Android Option Menu Example

Published by Shivam Pandey on

Android options menu are the primary menus of android. They can be used for settings, search, delete item etc.

Here we are going to see example of option menu.

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

Android Option Menu Example

Let’s see how to create menu in android.  Let’s see the simple option menu example that contains four menu items.

STEP – 1

activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Inbox">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/mail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ec3976"></androidx.appcompat.widget.Toolbar>
</RelativeLayout>

STEP – 2

menu_main.xml :

It contains four items as shown below. It is created automatically inside the res/menu directory.

 

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menu1"
        android:title="Profile"></item>
    <item android:id="@+id/menu2"
        android:title="Contact"></item>
    <item android:id="@+id/menu3"
        android:title="New Group"></item>
    <item android:id="@+id/menu4"
        android:title="Settings"></item>

</menu>

STEP – 3 

Activity  Main class :

package com.shivam.practiceapp.Activities;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.shivam.practiceapp.Adapters.InboxAdapter;
import com.shivam.practiceapp.R;

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

public class MainActivitys extends AppCompatActivity {

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inbox);
        toolbar=findViewById(R.id.mail);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.ibox,menu);
        return true;


    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.menu1:

                Toast.makeText(getApplicationContext(),"Profile",Toast.LENGTH_LONG).show();
                break;
            case R.id.menu2:
                Toast.makeText(getApplicationContext(),"Contact",Toast.LENGTH_LONG).show();
                break;
            case R.id.menu3:
                Toast.makeText(getApplicationContext(),"New Group",Toast.LENGTH_LONG).show();
                break;
            case R.id.menu4:
                Toast.makeText(getApplicationContext(),"Setting",Toast.LENGTH_LONG).show();
                break;
        }
        return true;
    }
}


Output :

Output without clicking on the menu button.

Output after clicking on the menu button.

In case of any query, you can comment that below or you can connect with the author on 

You can connect with the author on LINKEDIN and TWITTER

VISIT OUR WEBSITE BRIGHTERBEES FOR MORE INTERESTING STUFFS ABOUT THIS DEVELOPMENT.

IF YOU WANT TO KNOW ABOUT XML click here.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US