JCLaHoot JCLaHoot - 2 months ago 19
Android Question

I want to use an Overflow Menu in my Action Bar / AppCompat Toolbar

I'm trying to use Android

ActionBar
in my app, and have a sign-out option that's hidden away in the overflow menu.

I've tried a bunch of different things, but nothing seems to give me what I want without using a hacked solution. There's a lot of documentation out there, but it's confusing because most of it is only relevant to very old versions of Android, and when you try applying the same concepts, they don't work anymore or work differently. Normally I wouldn't ask a question that has a bunch of similar questions, but I haven't found any good resources on the topic that weren't outdated.

This is in my
Activity
layout

<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:titleTextColor="@android:color/white"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


And this is in my
Activity
's
onCreate()
method

// sets up activity toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);

myToolbar.showOverflowMenu();
myToolbar.setTitleTextColor(R.color.lightPrimaryText);


I've also tried inflating a menu xml file from the
onCreateOptionsMenu()
, but that also didn't give me the results I wanted.

Answer

Define a Menu for your Toolbar in the res/menu resource folder, for example:

toolbar_menu.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activity.MainActivity">

    <item
        android:id="@+id/action_sign_out"
        android:icon="@drawable/icon_sign_out"
        android:title="@string/toolbar_sign_out"
        app:showAsAction="never"/>
</menu>

Setting app:showAsAction="never" ensures that this MenuItem will not be shown in the Toolbar, but placed in the overflow menu instead.

The theme of your Activity should be (or derive from) one of the NoActionBar AppCompat themes (Theme.AppCompat.Light.NoActionBar for example).

In your Activity, set up your Toolbar:

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);

And override onCreateOptionsMenu():

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

You can override onOptionsItemSelected() to define the onClick behaviour of your MenuItem(s):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_sign_out: {
            // do your sign-out stuff
            break;
        }
        // case blocks for other MenuItems (if any)
    }
    return false;
}