Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

How to implement Options Menu in Android

The options menu is the primary collection of menu items for an activity. It's where you should place actions that have a global impact on the app, such as "Settings," "Search," "Refresh," etc.

Here's a step-by-step guide on how to implement an Options Menu in Android:

  1. Create the Menu XML:

    Inside your res directory, create a new directory named menu if it doesn't exist. Then, create a new XML file in this directory. For example, menu_options.xml.

    Add menu items to this XML:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/item1"
            android:title="Item 1"
            android:orderInCategory="100"
            android:showAsAction="never" />
        <item
            android:id="@+id/item2"
            android:title="Item 2"
            android:orderInCategory="200"
            android:showAsAction="never" />
    </menu>
    
  2. Override onCreateOptionsMenu:

    In your activity, override the onCreateOptionsMenu() method to inflate the menu XML:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_options, menu);
        return true;
    }
    
  3. Handle Menu Item Clicks:

    Override the onOptionsItemSelected() method in your activity:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
    
        // Handle menu item clicks here
        if (id == R.id.item1) {
            Toast.makeText(this, "Item 1 selected", Toast.LENGTH_SHORT).show();
            return true;
        } else if (id == R.id.item2) {
            Toast.makeText(this, "Item 2 selected", Toast.LENGTH_SHORT).show();
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
  4. Modify AndroidManifest.xml (Optional):

    If you want the options menu to display as a three-dot icon (overflow icon) even if the hardware menu button exists on a device, you can add the following line inside the <application> tag in your AndroidManifest.xml:

    <application
        android:theme="@style/Theme.AppCompat.Light"
        ...>
    

    Here, we're using the AppCompat theme which ensures that the overflow icon is shown. Adjust the theme as needed based on your app's design.

  5. Test Your Menu:

    Run your application and click on the three-dot icon (or the hardware menu button) to display the options menu. When you select an item, the respective toast message will appear.

Remember to adjust your menu items, icons, and actions as needed based on the requirements of your app.

  1. How to add Options Menu in Android Studio:

    • In your activity, override the onCreateOptionsMenu method to inflate the menu resource.
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options_menu, menu);
        return true;
    }
    
  2. Create Options Menu in Android example:

    • Create an XML file in the res/menu directory, for example, options_menu.xml:
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/menu_item_1"
            android:title="Option 1" />
        <item
            android:id="@+id/menu_item_2"
            android:title="Option 2" />
    </menu>
    
  3. Implementing action items in Android Options Menu:

    • Add action items to your options menu XML:
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/action_refresh"
            android:title="Refresh"
            android:icon="@drawable/ic_refresh" />
    </menu>
    
  4. Handling click events in Android Options Menu:

    • Override the onOptionsItemSelected method to handle click events:
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_1:
                // Handle Option 1 click
                return true;
            case R.id.menu_item_2:
                // Handle Option 2 click
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
  5. Android Options Menu XML layout example:

    • Example of an options menu XML layout (res/menu/options_menu.xml):
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/menu_item_1"
            android:title="Option 1" />
        <item
            android:id="@+id/menu_item_2"
            android:title="Option 2" />
    </menu>
    
  6. Code example for implementing Options Menu in Android:

    • Here's a complete example of an activity with an options menu:
    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) {
            getMenuInflater().inflate(R.menu.options_menu, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_item_1:
                    // Handle Option 1 click
                    return true;
                case R.id.menu_item_2:
                    // Handle Option 2 click
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
    }
    
    • Make sure to create the corresponding res/menu/options_menu.xml file.