display Icons In PopupMenu - Android User Interface

Android examples for User Interface:PopupMenu

Description

display Icons In PopupMenu

Demo Code


//package com.java2s;

import android.widget.PopupMenu;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    public static void displayIconsInPopup(PopupMenu popup) {
        try {/*w w  w.j  a  v a  2  s  . co  m*/
            Field[] fields = popup.getClass().getDeclaredFields();
            for (Field field : fields) {
                if ("mPopup".equals(field.getName())) {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popup);
                    Class<?> classPopupHelper = Class
                            .forName(menuPopupHelper.getClass().getName());
                    Method setForceIcons = classPopupHelper.getMethod(
                            "setForceShowIcon", boolean.class);
                    setForceIcons.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials