• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Context Menu - controlling position

RLScott

Newbie
I have a problem with my context menus. The items sensitive to long-press are items in a ListView of file names. The context menu has 3 items:
Delete this file
Rename this file
Display file info

It works fine except when I long-press an item near the top of the screen. Since the context menu likes to position itself by default just above the item that was long-pressed, the context menu is vertically compressed for items near the top of the screen, and all three menu choices are not visible at once. They can be scrolled to get to all three, but I don't want the user to have to scroll the menu. I would like to override the default positioning of the context menu. I realize that by doing so I will be breaking the visual association between the item the user long-pressed and the actions to be performed by the menu selections. But I can compensate for that by including the name of the item long-pressed in the context menu header, so the user will still know what item he long-pressed. So I just need to know how to override the default positioning of a context menu. My context menu is created entirely by code (no xml) as follows:

Code:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  if (v == list) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
    {
        File f = new File(path.get(info.position));
        menu.setHeaderTitle(FileExplorer.zapExtension(f.getName()));
        menu.add(Menu.NONE,0,0,"Delete this file");
        menu.add(Menu.NONE,1,1,"Rename this file");
        menu.add(Menu.NONE,2,2,"Display file info");
     }
  }
}

 
OK, I realized that using a Context Menu was not the only way to go for detecting long-press on items in a ListView. Instead of registering for Context Menu on the list, I did this just after ListView list is created:

Code:
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view,
                              int position, long id) {
         final File f = new File(path.get(position));
         AlertDialog.Builder builder = new AlertDialog.Builder(FileExplorer.this);
         builder.setTitle(f.getName());
         String item1 = f.isDirectory() ? "Delete this directory" : "Delete this file";
         String item2 = f.isDirectory() ? "Rename this directory" : "Rename this file";
         String item3 = "Cancel (" + {{some file info}} + ")";
         List<String> choiceItems = new ArrayList<String>();
         choiceItems.add(item1);
         choiceItems.add(item2);
         choiceItems.add(item3);
         final CharSequence[] charSequenceItems = choiceItems.toArray(new CharSequence[choiceItems.size()]);
         builder.setItems(charSequenceItems,
               new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                     AlertDialog.Builder builder2 = new AlertDialog.Builder(FileExplorer.this);
                     AlertDialog alert2;
                     final String name =  f.getName();
                     switch (which) {
                        case 0:       //..delete file or folder
                           {{confirm delete, then delete file}}
                           break;
                        case 1:    //..Rename file or folder
                           {{validate new file name, then rename}}
                           break;
                        case 2:       //..Cancel.  do nothing.
                           break;
                     }
                  }
               });        //..end of builder.setItems
         builder.create().show();
         return true;
      }    //..end of onItemLongClick
   });

The resulting AlertDialog is positioned in the middle of the screen and always shows all choices without any scrolling.
 
Back
Top Bottom