17

Menu items with title and icon when showAsAction is never

Hi, how can I make my menu items have icons as well when showAsAction is never ?

Elnoor
  • 3,401
  • 4
  • 24
  • 39
  • 1
    Why you make showAsAction equals never? If you want to show menu item with icons, value of showAsAction must be withText.`android:showAsAction = "withText"` – Almett Nov 18 '15 at 10:29
  • Because, that is the way I want them to appear. I do not want the menu item to appear in ActionBar but inside menu. When showAsAction is never, only text is displayed, but I want icon to appear as well, like the ones in picture. – Elnoor Nov 18 '15 at 10:39
  • There are some work around. I hope [this](http://stackoverflow.com/questions/15454995/popupmenu-with-icons) will help you. – Harry Mad Nov 18 '15 at 10:48
  • @Harry Mad I tried submenus. But it still didn't work. – Elnoor Nov 18 '15 at 21:37

5 Answers5

5

Use android:actionLayout in menu item tag to specify custom lyout (icon with text). Menu item will look like:

<item
    android:id="@+id/edit_menu"
    android:actionLayout="@layout/custom_edit_row"
    android:orderInCategory="100"
    android:title="@string/edit"
    app:showAsAction="always"></item>

Edit

custom_edit_row.xml

will be:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
style="@android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true">

<ImageView
    android:id="@+id/imageViewEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/edit_ic" />

<TextView
    android:id="@+id/tvEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Edit" />

Sk Saad Al Mahmud
  • 2,984
  • 2
  • 24
  • 22
1

Use this menu file, it worked fine for me.

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

    <item
        android:id="@+id/action_m"
        android:showAsAction="never"
        android:title="menu">
        <menu>
            <item
                android:id="@+id/action_one"
                android:icon="@android:drawable/ic_popup_sync"
                android:showAsAction="always"
                android:title="Sync"/>
            <item
                android:id="@+id/action_two"
                android:icon="@android:drawable/ic_dialog_info"
                android:title="About"/>
        </menu>
    </item>
</menu>
letsintegreat
  • 3,328
  • 4
  • 18
  • 39
Harry Mad
  • 488
  • 5
  • 17
  • Thanks a lot @Harry Mad, but the thing is that it is a submenu. But what I want is to display icon main menu item – Elnoor Nov 21 '15 at 06:44
1
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YourActivityName">

<item
    android:id="@+id/action_settings"
    android:icon="@android:drawable/btn_dialog"
    android:title="@string/action_settings"
    app:showAsAction="always">

    <menu>
        <item
            android:id="@+id/profile"
            android:icon="@drawable/common_google_signin_btn_icon_light_disabled"
            android:title="PROFILE"
            app:showAsAction="always" />
    </menu>

</item>

  • 3
    Can you explain your answer? A **good answer** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – B001ᛦ Jan 05 '17 at 12:38
0

In createOptionsMenu, set the icon and text to MenuItem.

Elaborate the problem a little more.

0

Just change root item of Harry Mad answer

    <item
    android:title="Ещё"
    app:showAsAction="always"
    android:icon="@drawable/ic_more_vert">
    ...