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.
I also need the device "menu" button to show the same menu.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With