How To Create Options Menu in Android

HOW-TO-CREATE-OPTIONS-MENU-IN-ANDROID-1.png

15th October, 2022

The Android menu provides some actions for the user. Such as settings, search, help etc. In this example you will learn how to create simple options menu in Android.

Step 1: Create New Project

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from the templates.

Step 2: Create a Menu XML File

To display options in activity, Create a menu xml file in menu resource directory. Create menu directory if there is no one.

Project ⇒ app ⇒ src ⇒ main ⇒ res ⇒ menu ⇒ my_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/action_color_red"
        android:icon="@drawable/ic_color_red"
        android:title="Red"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_color_green"
        android:icon="@drawable/ic_color_green"
        android:title="Green"
        app:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_color_blue"
        android:icon="@drawable/action_color_blue"
        android:title="Blue"
        app:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_color_black"
        android:icon="@drawable/ic_color_black"
        android:title="Black"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_color_yellow"
        android:icon="@drawable/ic_color_yellow"
        android:title="Yellow"
        app:showAsAction="never" />
</menu>

Take a look at the code above, the options menu item attributes are declared using the item element.

	android:icon="@drawable/ic_color_red"

This attribute is used to display the icon of a menu item.

	android:title="Red"

This is the title of the menu item.

app:showAsAction="always"

This attribute describes how to place an item in the app bar. The following table describes the possible value of the showAsAction attribute.

Value Description
always Always place this item in the app bar.
ifRoom Only place this item in the app bar if there is room for it.
never Never place this item in the app bar. show it in the overflow menu.
withText Show the title text along with item.
collapseActionView The action view related with this item is collapsible.

Step 3: Create and Handle Menu in Activity

Now open your activity and add options menu. The following code represents the implementation of options menu in the activity.

package com.codestringz.mytestapp;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{

    private TextView mTextView;

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

    private void setUIRef()
    {
        mTextView = findViewById(R.id.hintTextView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        //Inflate menu
        MenuInflater menuInflater = this.getMenuInflater();
        menuInflater.inflate(R.menu.my_options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        //Handle options menu item selection

        switch (item.getItemId())
        {
            case R.id.action_color_red:
                mTextView.setTextColor(Color.RED);
                return true;

            case R.id.action_color_green:
                mTextView.setTextColor(Color.GREEN);
                return true;

            case R.id.action_color_blue:
                mTextView.setTextColor(Color.BLUE);
                return true;

            case R.id.action_color_black:
                mTextView.setTextColor(Color.BLACK);
                return true;

            case R.id.action_color_yellow:
                mTextView.setTextColor(Color.YELLOW);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="www.CodestringZ.com" />

    <TextView
        android:id="@+id/hintTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="Change Text Color Using Options Menu"
        android:textSize="28sp" />

</RelativeLayout>

Output

Happy coding!

Leave a Reply