5
votes

enter image description here

How to add a menu popup when an action bar item is clicked (see screenshot)? I want the menu item to show an icon.

tHings I have tried:

  1. Setting actionProvider (support lib v7) for the action bar item. In the actionProvider, return null for onCreateActionView. In onPrepareSubMenu, populate the submenu. This works on Android 2.x but not Android 4.0, and for Android 2.x, there is no icon.

  2. In the actionProvider, create a imageview and on clicking, shows a PopupMenu, but popup menu has no icon, when I have specifically used setIcon to show it.

I don't understand why PopupMenu does not show any icon. I followed the "official" code as closely as possible but to no avail.

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/widget/ShareActionProvider.java#195

Please help! Thanks!

3

3 Answers

5
votes

Use popUpMenu ->>> Follow>

res/menu/horario.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item 
    android:id="@+id/menu_MudaDia"
    android:titleCondensed="Mudar Dia" 
    android:title="Mudar Dia" 
    android:icon="@drawable/ic_menu_popup" 
    android:showAsAction="always">
</item>

activity.class

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.horario, menu);

    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {



    switch (item.getItemId()) {
    case R.id.menu_MudaDia:
        View vItem = getActivity().findViewById(R.id.menu_MudaDia);
        PopupMenu popMenu = new PopupMenu(getActivity(), vItem);
        for (int i = 0; i < diaSemana.length; i++) 
        {
            popMenu.getMenu().add(0, i, i, diaSemana[i]);
        }

        popMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                DIA = diaSemana[item.getItemId()];
                atualizaGUI();
                return true;
            }
        });
        popMenu.show();
        break;

    default:
        break;
    }

    return super.onOptionsItemSelected(item);
}
1
votes

You can try creating a layout with the ImageView and TextView. Inflate that layout inside a PopUpWindow (refer : http://developer.android.com/reference/android/widget/PopupWindow.html).

Use the showAsDropDown(View actionBarIcon) method to show the menu on your actionbar icon click.

1
votes

I use support library v7 and works well.

- use ActionProvider

I use custom ActionProvider and it works well at 2.x and 4.x , code in onPrepareSubMenu

subMenu.clear();
// labels contain list item text.
int len = labels.length;  
for(int i = 0; i < len; i++) {
 subMenu.add(0, labels[i], i, labels[i])
        .setIcon(icons[i])
        .setOnMenuItemClickListener(new MineMenuItemClickListener());
}
super.onPrepareSubMenu(subMenu);

- about PopupMenu

PopupMenu don't show icon as default, but you can do your own PopupMenu and set icon display.

Like this man does CustomPopupMenu

The only modify is add mPopup.setForceShowIcon(true);