Add “options menu” to an AppCompatActivity (Android App Development)


Adding an “options menu” to an Android app’s Activity is quite simple.

This is an example of what an Android options menu looks like. Look at the icon that looks like 3 dots vertically stacked.

android options menu

My dev environment status as of writing this

Android Studio version: v. 2.1
OS: Mac OS X El Capitan v. 10.11
My Android app build status:

  • compileSdkVersion: 24
  • buildToolsVersion: 24.0.2
  • minSdkVersion: 16
  • targetSdkVersion: 24

Add “options menu” to an Android AppCompatActivity

#1 Create menu layout file

Create res/menu/main.xml, creating the menu folder if not yet existing.

Add the following to res/menu/main.xml:

#2 Enable menu by adding MenuInflater to the Activity

Add the following overriding method to your AppCompatActivity:

#3 Add listener for selected options menu item

Again, add the following overriding method to your AppCompatActivity. Replace the Toasts with whatever you need to do upon menu item selection:

That’s it! Your Android Activity should now have an options menu on the actionbar

My resources for adding my options menu to Activity

Adding the App Bar
Android SDK: Implement an Options Menu

Catzie

A Filipino who is a mobile & web developer on weekdays, and a baker on weekends. When I have time to spare, I blog, play video games, watch series (usually anime), cook dishes, draw, or spend a little time with pets. When I have the rare luxury of time, I get to travel and record song covers too! Follow on Twitter: @catzie

Leave a Reply

Your email address will not be published. Required fields are marked *