0

I'm trying to customize popup menu but failed to customize. I had tried many suggestions and solution but no luck to achieve this.

Please check and suggest me where I'm wrong...

I want to Create Popmenu like this: enter image description here

And I've tried these codes:

public PopupWindow popupDisplay() {

    final PopupWindow popupWindow = new PopupWindow(context);

    // inflate your layout or dynamically add view
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = inflater.inflate(R.layout.hs_popup_item, null);

    popupWindow.setFocusable(true);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        popupWindow.setElevation(4);
    }
    popupWindow.setClippingEnabled(true);

    return popupWindow;
}

Please guide me how I can add sub Title(descriptions) in the popup menu.

Garg
  • 2,731
  • 2
  • 36
  • 47
  • @Shubham Srivastava my question is not a duplicate i'm not asking for showing icon i want to display its description also like in image – Garg Apr 12 '18 at 12:45

1 Answers1

0
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(mContext, v);
popupMenu.inflate(R.menu.album_overflow_menu);

// Force icons to show
Object menuHelper;
Class[] argTypes;
try {
    Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
    fMenuHelper.setAccessible(true);
    menuHelper = fMenuHelper.get(popupMenu);
    argTypes = new Class[] { boolean.class };
    menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
} catch (Exception e) {
    // Possible exceptions are NoSuchMethodError and NoSuchFieldError
    //
    // In either case, an exception indicates something is wrong with the reflection code, or the 
    // structure of the PopupMenu class or its dependencies has changed.
    //
    // These exceptions should never happen since we're shipping the AppCompat library in our own apk, 
    // but in the case that they do, we simply can't force icons to display, so log the error and
    // show the menu normally.

    Log.w(TAG, "error forcing menu icons to show", e);
    popupMenu.show();
    return;
}

popupMenu.show();
}
Sandeep Kumar
  • 350
  • 4
  • 18