Forces a PopupMenu to display it's icons - Android User Interface

Android examples for User Interface:PopupMenu

Description

Forces a PopupMenu to display it's icons

Demo Code


import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.widget.PopupMenu;

public class Main {
  /**/*from ww  w. ja v  a2  s .  c  o m*/
   * Forces a popup menu to display it's icons
   * 
   * @param popup
   */
  public static void forcePopupMenuIcons(PopupMenu popup) {
    try {
      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