1

Following the docs about Android's menus, I was able to set a custom layout for one of my action bar's items (see item alwaysThere). For another item however, it won't show the custom version but always the standard text (see 'subMenuItem'). The difference between these two items is that the 2nd one is inside of a nested submenu.

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

  <item
        android:id      ="@+id/alwaysThere"
        android:title   ="Always there"
        app:showAsAction="always"
        app:actionLayout="@layout/customlayout" />

  <item
        android:title="Root"
        android:icon="@drawable/ic_people_white_24dp"
        app:showAsAction="always">
    <menu>
        <item
                  android:id     ="@+id/subMenuItem"
                  android:title  ="Submenu item"
            app:actionLayout="@layout/customlayout" />
[...]

Here's the result:

enter image description here

versus

enter image description here

I have alternatively tried to inflate the layout manually and use setActionView() when creating the options menu. Same result: working for the root item but not for items in the submenu, even if explicitly calling expandActionView().

What do I have to do to make the submenu entries also use the custom layout?

Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • you can use `PopupWindow` for that – AskNilesh Mar 08 '18 at 10:13
  • Any references, examples or more details? – Krumelur Mar 08 '18 at 10:14
  • Just wondering if this here is what answers my question: https://stackoverflow.com/questions/35490999/can-i-use-a-actionlayout-on-the-overflow-menu-of-android-support-v7-widget-toolb/35732788 - if it's in a submenu, it's not an action and thus no action view will be used. – Krumelur Mar 08 '18 at 10:15

1 Answers1

2

You can use PopupWindow. Follow these steps.

Create menu file like this:

<?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/action_menu"
        android:icon="@drawable/menu"
        android:title=""
        app:showAsAction="always">
 </item>

</menu>

Then displayPopupWindow inside like this onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.action_menu:
            displayPopupWindow(findViewById(R.id.action_menu));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Create pop-up window:

private void displayPopupWindow(View view) {
    PopupWindow popup = new PopupWindow(this);
            View layout = getLayoutInflater().inflate(R.layout.custom_popup_menu_layout, null);
            popup.setContentView(layout);
            popup.setOutsideTouchable(true);
            popup.showAsDropDown(view, 0, 0);
            popup.setFocusable(true);
    popup.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • I don't see how this would use my `customLayout` for the entries in the popup menu. Also, while it is nice to have this code, what is your answer to my question? Are you saying that custom layouts *cannot* be used directly and the only way to get them is to not use the standard behaviour and instead show a non-standard popup? – Krumelur Mar 08 '18 at 10:27
  • @Krumelur **AFAIK** i have same situation in my previous project I'm not able add custom layout in inside menu item so i have use **`PopupWindow`** – AskNilesh Mar 08 '18 at 10:28
  • I'm still not seeing how this would solve my problem of custom *items*. I don't want a custom layout for the popup but for individual items displayed inside of it. – Krumelur Mar 08 '18 at 10:42
  • @Krumelur inside `custom_popup_menu_layout` you can make layout of your item as you want – AskNilesh Mar 08 '18 at 10:47