ActionMenuView doesn't sowing menu items as Icons. (Only showing as overflow menu)
from the CommonsWare Community archivesAt January 14, 2019, 6:30pm, _shahdhaval asked:
I am trying to implement menu items using ActionMenuView. I have tried following every resource out there but couldn’t able to find the solution to my problem.
After inflating the menu items in ActionMenuView, I got the three dots (overflow) menu containing my menu items. But it doesn’t show the icons even though I have used app:showAsAction="always"
Following is the code I have tried till now.
[A] menu_main.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/menuItemSetting"
android:title="Settings"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="@drawable/ic_settings_white"/>
</menu>
[B] layout_toolbar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarMain"
android:layout_width="0dp"
app:titleTextColor="@android:color/white"
android:background="@color/colorPrimary"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<android.support.v7.widget.ActionMenuView
android:id="@+id/actionMenuViewMain"
android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="0dp"/>
</android.support.constraint.ConstraintLayout>
[C] Following code is used to inflate the menu:
private var actionMenuViewMain: ActionMenuView = findViewById(R.id.actionMenuViewMain)
val menu = actionMenuViewMain.menu
val menuInflater = MenuInflater(getContext())
menuInflater.inflate(R.menu.menu_main, menu)
Following is the output I got after using the above code:
Please let me know what I am doing wrong. Thank you.
At January 14, 2019, 7:03pm, mmurphy replied:
I have never used ActionMenuView
directly.
My best guess is that you should call getMenuInflater()
on your AppCompatActivity
, rather than using MenuInflater(getContext())
, to get your MenuInflater
. The one you are creating might not know about AppCompat.
Beyond that, here are some things to try:
- Remove
android:orderInCategory="100"
(you only have one item at the moment, so this is not doing anything for you) - Remove the
Toolbar
(not sure why you are using both that andActionMenuView
) - Add
android:showAsAction="always"
(this really should not be necessary, but…)