Android BottomAppBar dynamic submenu configuration

52 views Asked by At

Short version:

I'm looking for a BottomAppBar's equivalent of onPreprareOptionMenu(), so that I may dynamically configure (enable/disable) submenu items.

Full version:

I have a layout.xml:

<androidx.coordinatorlayout.widget.CoordinatorLayout>
    ...
    <com.google.android.material.bottomappbar.BottomAppBar
        ...
        app:menu="@menu/menu"
        />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

and menu.xml:

<menu>
    <item android:id="@+id/action"
        ...
        >
        <menu>
            <item android:id="@+id/action_1"
                ...
                />
            <item android:id="@+id/action_2"
                ...
                />
        </menu>
    </item>
</menu>

Now, I would like to dynamically enable or disable submenus id/action_1 or id/action_2. Any ideas?

1

There are 1 answers

0
GrzegDev On

Once you have a reference to BottomAppBar (which can be obtained via findViewById() or Data Binding, for instance), the submenu items can be accesess:

bottomAppBar.menu
        .findItem(R.id.action).subMenu
        .findItem(R.id.action_1).isEnabled = false