Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Toolbar Popup menu not showing icons

I am trying to show a drop down menu for my toolbar which includes BOTH text and icons:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/menu_add"
        android:title="@string/menu.add"
        android:icon="@drawable/ic_add_black_24dp"
        app:showAsAction="always" />

    <item
        android:id="@+id/menu_edit"
        android:title="@string/menu.edit"
        android:icon="@drawable/ic_create_black_24dp"
        app:showAsAction="never" />

</menu>

The menu_add does show with the icon on the toolbar itself but the menu_edit only shows the text without the icon.

This answer: https://stackoverflow.com/a/19750717/197127 says that Google has removed it by design but does not refer to how you may override it.

Edit

I also need the device "menu" button to show the same menu.

like image 595
checklist Avatar asked Jun 15 '15 12:06

checklist


1 Answers

I created something like this:

@SuppressLint("RestrictedApi")
fun Menu.showIcons() {
    (this as? MenuBuilder)?.setOptionalIconsVisible(true)
}

and it works basically on any menu.

For toolbar, you can override onPrepareOptionsMenu in activity or fragment and before calling super just call menu.showIcons() or you can use it with PopupMenu like so PopupMenu(requireContext(), anchor).menu.showIcons().

like image 68
Kikju Avatar answered Sep 21 '22 02:09

Kikju