Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android open ContextMenu on short click + pass item clicked details

Tags:

android

 lv.setOnItemClickListener(new OnItemClickListener() {
             @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              TextView text = (TextView) view.findViewById(R.id.btitle);
              registerForContextMenu(text);
              view.showContextMenu();
              }
            });
       }

     @Override
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
       super.onCreateContextMenu(menu, v, menuInfo);
       TextView text = (TextView) v.findViewById(R.id.btitle);
       CharSequence itemTitle = text.getText();
       menu.setHeaderTitle(itemTitle);

       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.context_menu, menu);

     }

Hello,

I'm trying to open a contextMenu on short item click. I've managed to do so only if I add registerForContextMenu(getListView()); somewhere but this also triggers contextMenu on long click (which I don't want to happen).

  • Tried view.showContextMenu() but it doesn't do anything unless I add the registerForContextMenu(getListView()); .
  • Tried registering the clicked item first and then call the showContextMenu() but didn't do anything as well...

Also, I want to get the clicked item image + text so I can use them in the contextMenu.

Appreciate the help!

like image 818
Lior Iluz Avatar asked Sep 15 '10 22:09

Lior Iluz


4 Answers

I was able to open a context menu on button click with the following code:

public void onButtonClickEvent(View sender)
{
    registerForContextMenu(sender); 
    openContextMenu(sender);
    unregisterForContextMenu(sender);
}

Just set the onClick property of the button to onButtonClickEvent. A long click won't trigger the context menu, since it is unregistrered right after it is shown.

like image 168
Konrad Avatar answered Oct 18 '22 01:10

Konrad


Regarding opening a context menu on a short click:

The other solutions posted here didn't work for me because I was using a ListFragment. However the following solution seems to work quite nicely for both a ListFragment and a ListActivity (or just any old listview in general):

public void onListItemClick(ListView l, View v, int position, long id){
  l.showContextMenuForChild(v);   
}

It's much more simple and elegant. In fact, this is actually how the ListView class itself initiates the context menu on a long click.

like image 44
Kurtis Nusbaum Avatar answered Oct 18 '22 01:10

Kurtis Nusbaum


Solution:

@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);

      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
      long itemID = info.position;
      menu.setHeaderTitle("lior" + itemID);
    }

AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;

gives you more details about the list item clicked. Then you can use info.id, info.position and so on to retrieve the details and use them actions (edit, delete...).

like image 4
Lior Iluz Avatar answered Oct 18 '22 03:10

Lior Iluz


In my case I had BaseAdapter and implemented click on button for each listView item (in getView method):

ImageButton menuBtn = (ImageButton) convertView.findViewById(R.id.itemListMenu);
menuBtn.setOnClickListener(new android.view.View.OnClickListener() {
    public void onClick(View v) {
        parent.showContextMenuForChild(v);
    }
 });
like image 1
Shay Zalman Avatar answered Oct 18 '22 02:10

Shay Zalman