'ActionBar, Current Android Developer Instructions

I read the "Add the app bar" section of the Android Developers Guide. I just want the default bar with the app name and the "overflow" menu (three vertical dots).

I followed all the steps here, verbatim. There are only a few. I am using androidx since its 2022 and all that.

https://developer.android.com/training/appbar/setting-up

After calling setSupportActionBar() from activity main, the documentation says:

"Your app now has a basic action bar. By default, the action bar contains just the name of the app and an overflow menu."

I do indeed have a basic action bar with the app name, but NO overflow menu.

The methods available on an ActionBar object don't have anything like "display overflow menu" or "turn on/off overflow menu" or anything like that.

I'm guessing this might be some weird configuration error. I'm using Linux (Ubuntu 20.04), have the latest Android studio "Bumblebee", and using the Nexus 5X API 24 emulator.

I've watched a set of (older) YouTube videos on this really basic configuration. They all show the "overflow" menu.

It's kind of annoying because I only want to add it for the purpose of having an overflow menu!

Thanks in advance,

-Blue

(EDITED) OK, the simple answer. I suppose it should be obvious really... The overflow menu only appears if you have created a menu with at least one item (e.g. help) and that menu has been inflated in the Activity or Fragment that contains it.

So, to add a menu in XML (the recommended way), add a menu under the resources, menu folder (creating the menu folder if it doesn't exist):

In app/res/menu, create a new Menu resource. To do this, go to app/res/menu, click on the directory "menu", and then right-click, New, "Menu Resource File".

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/help"
        android:icon="@drawable/ic_action_menu_cake"
        android:title="@string/main_help"/>
</menu>

You will need to create an ic_help drawable icon in the above case, and put the main_help in the string resources file (res/values/strings.xml).

Add as many menu items as you want, but at least one is required for the "Overflow menu" (three vertical dots to the right) to appear in Android 11+.

Then in the activity, inflate the menu by overriding onCreateOptionsMenu(Menu menu)

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source