Options Menu In Android Studio

Ali Talha Çoban
3 min readJul 26, 2021

--

Hi everyone in this I will talk abaout Menu in Android Studio and how to use it in your projects.

How To Add ?

We need to create menu resource file into menu folder you created.

Now we created file that we’ll work on.Coming up next setting the options menu.Basically, we can form with below.

Now we have created the file on which we will edit. It is time to design our options menu. In its most basic form, we can create our menu as follows.

<?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_add_contact"
android:title="Add contact"
/>

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

<item android:id="@+id/action_about_us"
android:title="About us"
/>
</menu>

If we want some items in options menu to appear as a icon,we need to add showAsAction attribute like below.

<?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_add_contact"
android:icon="@drawable/ic_baseline_person_add_24"
android:title="Add contact"
app:showAsAction="ifRoom"/>

<item android:id="@+id/action_settings"
android:icon="@drawable/ic_baseline_settings_24"
android:title="Settings"
app:showAsAction="always"/>

<item android:id="@+id/action_about_us"
android:icon="@drawable/ic_baseline_info_24"
android:title="About us"
app:showAsAction="never"/>
</menu>

Here we can make it appear under all conditions by doing always, and by doing ifRoom we can set the icon to show whether there is room in the actionbar or not.
Finally, if we never do it, we can make it not appear under any circumstances (!!! As seen in the previous code, we have already chosen not to appear at all by not using the showAsAction property by default)

Options menu ready to use and now we need to attach menu to activity.

There are two override method for that operation.The first of these allows us to connect the options menu we created to our activity.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}

Our other method allows us to control what options menu item is clicked on.

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

// Tıklanan öğenin id'sini alıyoruz
int id = item.getItemId();

switch (id){
case R.id.action_contact:
//action_contact'a tıklandığında ne olacağını buraya giriyoruz
break;
case R.id.action_settings:
//action_settings'e tıklandığında ne olacağını buraya giriyoruz
break;
case R.id.action_about_us:
//action_about_us'a tıklandığında ne olacağını buraya giriyoruz
break;
}
return super.onOptionsItemSelected(item);
}

In this article, I tried to explain how to add the options menu feature in Android Studio. I hope it helped you. Do not forget to follow for such articles.

contact : cobanalitalha@gmail.com

--

--

Ali Talha Çoban
Ali Talha Çoban

Written by Ali Talha Çoban

Backend Developer | Node.js | Spring Boot | PostgreSQL | MongoDB | MySQL

No responses yet