List of usage examples for android.widget PopupMenu getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.pranavpandey.smallapp.sample.SmallAppSample.java
private void setupOptionMenu() { View header = LayoutInflater.from(this).inflate(R.layout.header, new LinearLayout(this), false); final View optionMenu = header.findViewById(R.id.option_menu); optionMenu.setOnLongClickListener(new View.OnLongClickListener() { @Override//from ww w . ja va2s . c om public boolean onLongClick(View v) { SmallTheme.getInstance().showHeaderHint(v, R.string.sas_options); return false; } }); optionMenu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupMenu popup = new PopupMenu(getContext(), optionMenu); 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(); } popup.getMenuInflater().inflate(R.menu.main, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.menu_settings: showSettings(); break; case R.id.menu_about: new AboutDialog(SmallAppSample.this, R.mipmap.ic_launcher, R.string.app_name, Type.LIST) .show(getRootView()); break; case R.id.menu_sources: SmallUtils.openLink(SmallAppSample.this, getRootView(), SOURCES_LINK, null, Type.GRID); break; } return true; } }); popup.show(); } }); getWindow().setHeaderView(header); }
From source file:im.vector.fragments.GroupsFragment.java
@SuppressLint("NewApi") private void displayGroupPopupMenu(final Group group, final View actionView) { final Context context = getActivity(); final PopupMenu popup; if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { popup = new PopupMenu(context, actionView, Gravity.END); } else {//from w w w.j a v a 2 s . com popup = new PopupMenu(context, actionView); } popup.getMenuInflater().inflate(R.menu.vector_home_group_settings, popup.getMenu()); CommonActivityUtils.tintMenuIcons(popup.getMenu(), ThemeUtils.getColor(context, R.attr.settings_icon_tint_color)); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(final MenuItem item) { switch (item.getItemId()) { case R.id.ic_action_select_remove_group: { leaveOrReject(group.getGroupId()); break; } } return false; } }); // force to display the icon 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) { Log.e(LOG_TAG, "## displayGroupPopupMenu() : failed " + e.getMessage()); } popup.show(); }
From source file:im.vector.adapters.VectorMessagesAdapter.java
/** * The user taps on the action icon./*www . j a v a2s . c o m*/ * @param event the selected event. * @param textMsg the event text * @param anchorView the popup anchor. */ @SuppressLint("NewApi") private void onMessageClick(final Event event, final String textMsg, final View anchorView) { final PopupMenu popup = (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) ? new PopupMenu(mContext, anchorView, Gravity.END) : new PopupMenu(mContext, anchorView); popup.getMenuInflater().inflate(R.menu.vector_room_message_settings, popup.getMenu()); // force to display the icons 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) { Log.e(LOG_TAG, "onMessageClick : force to display the icons failed " + e.getLocalizedMessage()); } Menu menu = popup.getMenu(); // hide entries for (int i = 0; i < menu.size(); i++) { menu.getItem(i).setVisible(false); } menu.findItem(R.id.ic_action_view_source).setVisible(true); menu.findItem(R.id.ic_action_vector_permalink).setVisible(true); if (!TextUtils.isEmpty(textMsg)) { menu.findItem(R.id.ic_action_vector_copy).setVisible(true); menu.findItem(R.id.ic_action_vector_quote).setVisible(true); } if (event.isUploadingMedias(mMediasCache)) { menu.findItem(R.id.ic_action_vector_cancel_upload).setVisible(true); } if (event.isDownloadingMedias(mMediasCache)) { menu.findItem(R.id.ic_action_vector_cancel_download).setVisible(true); } if (event.canBeResent()) { menu.findItem(R.id.ic_action_vector_resend_message).setVisible(true); if (event.isUndeliverable()) { menu.findItem(R.id.ic_action_vector_redact_message).setVisible(true); } } else if (event.mSentState == Event.SentState.SENT) { // test if the event can be redacted boolean canBeRedacted = !mIsPreviewMode; if (canBeRedacted) { // oneself message -> can redact it if (TextUtils.equals(event.sender, mSession.getMyUserId())) { canBeRedacted = true; } else { // need the mininum power level to redact an event Room room = mSession.getDataHandler().getRoom(event.roomId); if ((null != room) && (null != room.getLiveState().getPowerLevels())) { PowerLevels powerLevels = room.getLiveState().getPowerLevels(); canBeRedacted = powerLevels.getUserPowerLevel(mSession.getMyUserId()) >= powerLevels.redact; } } } menu.findItem(R.id.ic_action_vector_redact_message).setVisible(canBeRedacted); if (Event.EVENT_TYPE_MESSAGE.equals(event.type)) { Message message = JsonUtils.toMessage(event.getContentAsJsonObject()); // share / forward the message menu.findItem(R.id.ic_action_vector_share).setVisible(true); menu.findItem(R.id.ic_action_vector_forward).setVisible(true); // save the media in the downloads directory if (Message.MSGTYPE_IMAGE.equals(message.msgtype) || Message.MSGTYPE_VIDEO.equals(message.msgtype) || Message.MSGTYPE_FILE.equals(message.msgtype)) { menu.findItem(R.id.ic_action_vector_save).setVisible(true); } // offer to report a message content menu.findItem(R.id.ic_action_vector_report) .setVisible(!mIsPreviewMode && !TextUtils.equals(event.sender, mSession.getMyUserId())); } } // display the menu popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(final MenuItem item) { // warn the listener if (null != mVectorMessagesAdapterEventsListener) { mVectorMessagesAdapterEventsListener.onEventAction(event, textMsg, item.getItemId()); } // disable the selection mHighlightedEventId = null; notifyDataSetChanged(); return true; } }); popup.show(); }
From source file:im.neon.adapters.VectorMessagesAdapter.java
/** * The user taps on the action icon.//from ww w.j a va 2 s. com * @param event the selected event. * @param textMsg the event text * @param anchorView the popup anchor. */ @SuppressLint("NewApi") private void onMessageClick(final Event event, final String textMsg, final View anchorView) { final PopupMenu popup = (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) ? new PopupMenu(mContext, anchorView, Gravity.END) : new PopupMenu(mContext, anchorView); popup.getMenuInflater().inflate(R.menu.vector_room_message_settings, popup.getMenu()); // force to display the icons 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) { Log.e(LOG_TAG, "onMessageClick : force to display the icons failed " + e.getLocalizedMessage()); } Menu menu = popup.getMenu(); // hide entries for (int i = 0; i < menu.size(); i++) { menu.getItem(i).setVisible(false); } menu.findItem(R.id.ic_action_view_source).setVisible(true); menu.findItem(R.id.ic_action_vector_permalink).setVisible(true); if (!TextUtils.isEmpty(textMsg)) { menu.findItem(R.id.ic_action_vector_copy).setVisible(true); menu.findItem(R.id.ic_action_vector_quote).setVisible(true); } if (event.isUploadingMedias(mMediasCache)) { menu.findItem(R.id.ic_action_vector_cancel_upload).setVisible(true); } if (event.isDownloadingMedias(mMediasCache)) { menu.findItem(R.id.ic_action_vector_cancel_download).setVisible(true); } if (event.canBeResent()) { menu.findItem(R.id.ic_action_vector_resend_message).setVisible(true); if (event.isUndeliverable() || event.isUnkownDevice()) { menu.findItem(R.id.ic_action_vector_redact_message).setVisible(true); } } else if (event.mSentState == Event.SentState.SENT) { // test if the event can be redacted boolean canBeRedacted = !mIsPreviewMode && !TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTION); if (canBeRedacted) { // oneself message -> can redact it if (TextUtils.equals(event.sender, mSession.getMyUserId())) { canBeRedacted = true; } else { // need the mininum power level to redact an event Room room = mSession.getDataHandler().getRoom(event.roomId); if ((null != room) && (null != room.getLiveState().getPowerLevels())) { PowerLevels powerLevels = room.getLiveState().getPowerLevels(); canBeRedacted = powerLevels.getUserPowerLevel(mSession.getMyUserId()) >= powerLevels.redact; } } } menu.findItem(R.id.ic_action_vector_redact_message).setVisible(canBeRedacted); if (Event.EVENT_TYPE_MESSAGE.equals(event.getType())) { Message message = JsonUtils.toMessage(event.getContentAsJsonObject()); // share / forward the message menu.findItem(R.id.ic_action_vector_share).setVisible(true); menu.findItem(R.id.ic_action_vector_forward).setVisible(true); // save the media in the downloads directory if (Message.MSGTYPE_IMAGE.equals(message.msgtype) || Message.MSGTYPE_VIDEO.equals(message.msgtype) || Message.MSGTYPE_FILE.equals(message.msgtype)) { menu.findItem(R.id.ic_action_vector_save).setVisible(true); } // offer to report a message content menu.findItem(R.id.ic_action_vector_report) .setVisible(!mIsPreviewMode && !TextUtils.equals(event.sender, mSession.getMyUserId())); } } // e2e menu.findItem(R.id.ic_action_device_verification).setVisible(mE2eIconByEventId.containsKey(event.eventId)); // display the menu popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(final MenuItem item) { // warn the listener if (null != mVectorMessagesAdapterEventsListener) { mVectorMessagesAdapterEventsListener.onEventAction(event, textMsg, item.getItemId()); } // disable the selection mHighlightedEventId = null; notifyDataSetChanged(); return true; } }); // fix an issue reported by GA try { popup.show(); } catch (Exception e) { Log.e(LOG_TAG, " popup.show failed " + e.getMessage()); } }