XML : Android: Creating context menu from long press on popup menu item

I'm trying to find a way to create a floating context menu when an item in my fragment's popup menu is long-pressed. When the menu icon in my layout is clicked, the popup menu appears and displays a list of files stored in a local directory to the app. When one of these files are long-pressed, i'd like a floating context menu to appear that gives the user the option of renaming or deleting the selected file.

According to the Android developer guide, floating context menus can be generated by passing a View (such as a ListView) into registerForContextMenu(). The programmer must then implement onCreateContextMenu() in the desired Activity or Fragment, i.e:

@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); }

I'm confused because registerForContextMenu() accepts a View as a parameter, but I'm trying to register the context menu to all the items in my Popup Menu instead of something like a ListView or a GridView. I've included my PopupMenu code below. How can I achieve a floating context menu when my popup menu items are long-pressed?

  popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {                      @Override                      public boolean onMenuItemClick(MenuItem item) {                          switch (item.getItemId()) {                              case R.id.new_track:                                  trackSelectButton.setText("...");                                  Toast.makeText(getActivity(), "Name your new track.", Toast.LENGTH_SHORT).show();                                  txtTrackName.setVisibility(txtTrackName.VISIBLE);                                  return true;                              default:                                  selectedTrackName = (item.getTitle().toString());                                  trackSelectButton.setText(selectedTrackName);                                  for (int i = 0; i < trackListing.length; i++) {      //add a menu item for each existing track                                      if (trackListing[i].getName().equals(selectedTrackName)) {                                          selectedTrack = trackListing[i];                                          AudioRecorder.setFile(selectedTrack);                                      }                                  }                                  return true;                          }                      }                  });                    MenuInflater popupInflater = popup.getMenuInflater();                  popupInflater.inflate(R.menu.popup_menu_track_selection, popup.getMenu());                  popup.show();    

No comments:

Post a Comment