Android, Xml

Option Menu in android | Option Menu example in android

Android option menu

Android option menu

Android option menu :

Android menu is a set of options, the user can select from to perform an action. Android option menu is the primary collection of menu items for an activity. It is the place where we implement the actions that have a global impact on the app, such as Search, Settings and Delete. Option menu appears in the app bar and provides the primary options.

1. Create a New Project :

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from templates to create an option menu in android using android studio.

2.  Implement support library :

Implement android design support library inside your project build.gradle file.

implementation 'com.android.support:design:28.0.0

Try to add the latest version of the library. Once that’s done, save the file and perform a Gradle sync.

3. Add TextView to your activity_main.xml or main layout

Add TextView to your main layout. 

<TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Options Menu"
        android:layout_centerInParent="true"
        android:textSize="18sp"/>

Now open res -> layout -> activity_main.xml  file and just implement the following xml code .

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Options Menu"
        android:layout_centerInParent="true"
        android:textSize="18sp"/>

</RelativeLayout>

4. Define a XML menu resource file :

Create a menu resource file named as options_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/notification"
        android:title="@string/notification"/>
    <item android:id="@+id/help"
        android:title="@string/help"/>
    <item android:id="@+id/setting"
        android:title="@string/setting"/>
    <item android:id="@+id/logout"
        android:title="@string/logout"/>
</menu>

5. Inflating the menu :

To inflate the menu resource (defined in XML) into the Menu override the onCreateOptionsMenu() method in your activity or fragment.

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

        return true;
    }

6. Handling menu item clicks :

When the user selects an item from the options menu (including action items in the app bar), the system calls activity’s onOptionsItemSelected() method. The onOptionsItemSelected() method can handle all the menu item clicks in one place, and determine which menu item was clicked.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()){
            case R.id.notification :  
            Toast.makeText(this,"Notification",Toast.LENGTH_SHORT).show();
                break;
            case R.id.help :
                Toast.makeText(this,"Help",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting :
                Toast.makeText(this,"Setting",Toast.LENGTH_SHORT).show();
                break;
            case R.id.logout :
                Toast.makeText(this,"Logout",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;

        }
        return super.onOptionsItemSelected(item);
    }

7.  Now open your MainActivity.java and add following code :

package net.technxt.optionmenu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.text);


    }

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

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item click
        switch (item.getItemId()){
            case R.id.notification :               
            Toast.makeText(this,"Notification",Toast.LENGTH_SHORT).show();
                break;
            case R.id.help :
                Toast.makeText(this,"Help",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting :
                Toast.makeText(this,"Setting",Toast.LENGTH_SHORT).show();
                break;
            case R.id.logout :
                Toast.makeText(this,"Logout",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;

        }
        return super.onOptionsItemSelected(item);
    }
}

Now run the project/app to test Option Menu in android app through android device or any Emulator .

Leave a Reply

Created by Bhashkar Poddar

%d bloggers like this: