1

I'm developing an android app in Java and want to add a custom regular MaterialToolbar with a settings icon on the right side.
I had a look at the MDC-Android how to implement it.
In the Android Studio design preview everything looked just fine, but when I ran the app on the emulator, the AppBar showed up without the custom menu (settings icon).

My AppBarLayout:

<com.google.android.material.appbar.AppBarLayout
    ...
    >

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/topAppBar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:menu="@menu/custom_menu"
        app:title="@string/app_name" />

</com.google.android.material.appbar.AppBarLayout>

custom_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">
    <item
        android:id="@+id/nav_settings"
        android:contentDescription="@string/settings_description"
        android:icon="@drawable/ic_baseline_settings_24"
        android:title="@string/settings"
        app:showAsAction="ifRoom" />
</menu>

After some time I figured out, that I still have to add following code to my AppCompatActivity, to actually show the custom menu (not only in the preview):

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

I thought it would be enough to add the app:menu attribute to my MaterialToolbar, but it seems like it just affects the preview. (The running app also shows the custom menu if the app:menu attribute isn't used)
I couldn't find anything about what app:menu except for some related methods (Found here), but this didn't really help me.

Is the app:menu attribute just used for the Android Studio preview?
If yes => why isn't it in the tools namespace (tools:menu)
If no => what other use cases does app:menu have?

Steffen
  • 501
  • 1
  • 14
  • 2
    You've apparently set the `Toolbar` as the support `ActionBar`, which is why overriding `onCreateOptionsMenu()` "fixed" it. The `ActionBar`'s menu is handled by the `Activity` as its options menu, so when you'd set the `Toolbar` as the `ActionBar`, the `Activity` was clearing whatever menu the `Toolbar` already had and adding its options menu, which was empty 'cause you hadn't overridden `onCreateOptionsMenu()` to inflate it. If you don't set the `Toolbar` as the support `ActionBar`, its `app:menu` attribute will work as you were expecting, but then obviously it won't act as the `ActionBar`. – Mike M. Dec 01 '22 at 20:03
  • 1
    I feel like my comment above might be a bit confusing, so just to clarify: the `app:menu` attribute in your setup is actually working, but it's being replaced by the `Activity` for its own options menu when you set the `Toolbar` as the support `ActionBar`. – Mike M. Dec 02 '22 at 06:37
  • 1
    Thanks, @MikeM.! You assumed right, I set the Toolbar as the `supportActionBar`, otherwise I got an exception running the App `IllegalStateException` (Activity does not have an ActionBar set via setSupportActionBar()). I also use an `BottomNavigationView` with the `app:menu` attribute, and now recognized that my `Toolbar` and `BottomNavigationView` interfere with each other. I will now have a closer look in the documentation about the ActionBar, thanks! – Steffen Dec 02 '22 at 09:47

0 Answers0