# OptionsMenu

In this tutorial we are going to create two types of options menu in android. First one is which there is 3 dots is present in actionbar and if you click that it will show a popup menu with a list of actions.

You can create a menu by right clicking on the res folder then android resource file. Give the name menu and set resource type menu

**Step 01**

![](https://111087263-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_Lt0fyRsHcS_4ZagFx%2F-LmTAlQ710R9GVprk6ho%2F-LmTEnrvXnZOK7o_8u1C%2FScreenshot%20\(2\).png?alt=media\&token=c4b09c51-f565-45a3-88b3-ffdfd1ac96fb)

**Step 02**

![](https://111087263-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_Lt0fyRsHcS_4ZagFx%2F-LmTAlQ710R9GVprk6ho%2F-LmTEqATaSf-gttbkIH0%2FScreenshot%20\(3\).png?alt=media\&token=d8143e31-0285-4c91-b439-8e207228d473)

Your menu file should look like this

{% code title="menu.xml" %}
```markup
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/settings"
        android:title="Settings" />
</menu>

```
{% endcode %}

Now inside MainActivity implement **onCreateOptionsMenu** and **onOptionsItemSelected** methods or you can simply add below code to MainActivityFile below **onCreate** Method.

{% code title="MainActivity.java" %}
```java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.settings:
            Toast.makeText(this, "clicked settings", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
```
{% endcode %}

If you have a one menu item then it is best practice to show it directly on toolbar with an icon like below

![](https://111087263-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_Lt0fyRsHcS_4ZagFx%2F-LmTHOTrIpoNJbkx2Cfb%2F-LmTIB4Mw7GAGPz76TjX%2Fdevice-2019-08-17-120817.png?alt=media\&token=b2199566-4bea-4cde-a89f-e06621061a0b)

First we need the settings icon you see above for that right click on drawable -> new -> vector asset. click on the android icon and search for settings and make sure to select white color and atlast add it to drawable. now in our **menu.xml** file add following code.

{% code title="menu.xml" %}
```markup
<?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/settings"
        app:showAsAction="always"
        android:icon="@drawable/ic_settings_black_24dp"
        android:title="Settings" />
</menu>
```
{% endcode %}

Now if you run you will get something like this

![](https://111087263-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-L_Lt0fyRsHcS_4ZagFx%2F-LmTHOTrIpoNJbkx2Cfb%2F-LmTJPP1fl1yep0JjBXn%2Fmenu_advanced.gif?alt=media\&token=805488f5-81b7-48d3-a621-2cddc8a46196)

Complete code [here](https://github.com/jobinjj/OptionsMenu)
