Popup menu with recyclerView
suggest changeput this code inside your ViewHolder
note: In this code I am using btnExpand
click-event, for whole recyclerview
click event you can set listener to itemView object.
public class MyViewHolder extends RecyclerView.ViewHolder{ CardView cv; TextView recordName, visibleFile, date, time; Button btnIn, btnExpand; public MyViewHolder(final View itemView) { super(itemView); cv = (CardView)itemView.findViewById(R.id.cardview); recordName = (TextView)itemView.findViewById(R.id.tv_record); visibleFile = (TextView)itemView.findViewById(R.id.visible_file); date = (TextView)itemView.findViewById(R.id.date); time = (TextView)itemView.findViewById(R.id.time); btnIn = (Button)itemView.findViewById(R.id.btn_in_out); btnExpand = (Button) itemView.findViewById(R.id.btn_expand); btnExpand.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupMenu popup = new PopupMenu(btnExpand.getContext(), itemView); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.action_delete: moveFile(recordName.getText().toString(), getAdapterPosition()); return true; case R.id.action_play: String valueOfPath = recordName.getText().toString(); Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File(valueOfPath); intent.setDataAndType(Uri.fromFile(file), "audio/*"); context.startActivity(intent); return true; case R.id.action_share: String valueOfPath = recordName.getText().toString(); File filee = new File(valueOfPath); try { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setType("audio/*"); sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(filee)); context.startActivity(sendIntent); } catch (NoSuchMethodError | IllegalArgumentException | NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return true; default: return false; } } }); // here you can inflate your menu popup.inflate(R.menu.my_menu_item); popup.setGravity(Gravity.RIGHT); // if you want icon with menu items then write this try-catch block. try { Field mFieldPopup=popup.getClass().getDeclaredField("mPopup"); mFieldPopup.setAccessible(true); MenuPopupHelper mPopup = (MenuPopupHelper) mFieldPopup.get(popup); mPopup.setForceShowIcon(true); } catch (Exception e) { } popup.show(); } }); } }
alternative way to show icons in menu
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) { }
Here is the output:

Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents