Creating a menu after an event with a long click in list view

I have a list view related to a database showing all entries. I want the menu to be displayed if the user long clicks an item in the list (record in the database), showing options for editing or deleting the record. How can i do this.

So far, I tried using the onItemLongClick listener and a toast in it, showing the id of the long click.

@Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { String res = Long.toString(id); Toast toast = Toast.makeText(this, res, Toast.LENGTH_SHORT); toast.show(); return true; } 
+67
android listview menu long-click
Jun 20 '13 at 7:09
source share
7 answers

Instead of onItemLongClick you can use

 public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo) { ... } 

where you adjust the options for editing and deleting or what you need.

Actions for an item selected from the context menu can be processed in

 public boolean onContextItemSelected(final MenuItem item) 

Read more about the context menu here .

For a step-by-step guide, visit here .

+49
Jun 20 '13 at 7:15
source share

First you need to register your context menu as a list.

 lv = (ListView) findViewById(R.id.list_view); registerForContextMenu(lv); 

Then just override the activity methods.

 /** * MENU */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); if (v.getId()==R.id.list_view) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_list, menu); } } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch(item.getItemId()) { case R.id.add: // add stuff here return true; case R.id.edit: // edit stuff here return true; case R.id.delete: // remove stuff here return true; default: return super.onContextItemSelected(item); } } 

Here is an example of the menu_list.xml file (you should put the file in the res / menu folder).

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/add" android:icon="@android:drawable/ic_menu_add" android:title="@string/menu_add" /> <item android:id="@+id/edit" android:icon="@android:drawable/ic_menu_edit" android:title="@string/menu_edit" /> <item android:id="@+id/delete" android:icon="@android:drawable/my_icon_delete" android:title="@string/menu_delete" /> </menu> 

Hope this helps.

+111
Jun 20 '13 at 7:22
source share

Another approach:

 //Deleted individual cart items //on list view cell long press cartItemList.setOnItemLongClickListener (new OnItemLongClickListener() { @SuppressWarnings("rawtypes") public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) { final CharSequence[] items = { "Delete" }; AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Action:"); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { cart = cartList.get(position); db.removeProductFromCart(context, cart); new AlertDialog.Builder(context) .setTitle(getString(R.string.success)) .setMessage(getString(R.string.item_removed)) .setPositiveButton("Done", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(CartDetailsActivity.this, HomeScreen.class); startActivity(intent); } }) .show(); } }); AlertDialog alert = builder.create(); alert.show(); //do your stuff here return false; } }); 
+5
Dec 12 '13 at 9:25
source share

You can call Activity.openOptionsMenu() in the click element method

here. http://developer.android.com/reference/android/app/Activity.html#openOptionsMenu%28%29

+2
Jun 20 '13 at 7:13
source share

**

after registering your context menu as a list

** override onCreateContextMenu Method similar to this

 @Override public void onCreateContextMenu(ContextMenu menu,View v, ContextMenu.ContextMenuInfo menuInfo){ if (v.getId() == R.id.listView){ AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)menuInfo; MenuItem mnu1=menu.add(0,0,0,"Delete"); MenuItem mnu2=menu.add(0,1,1,"Share"); } } 

then coding for each item that can be selected

  @Override public boolean onContextItemSelected(MenuItem menuItem){ AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)menuItem.getMenuInfo(); switch (menuItem.getItemId()) { case 0: Toast.makeText(this, "Delete Selected", Toast.LENGTH_LONG).show(); break; case 1: Toast.makeText(this, "Share Selected", Toast.LENGTH_LONG).show(); break; default: break; } return true; } 
+1
Dec 31 '16 at 17:40
source share

Use registerForContextMenu (); to associate a context menu with any view predictor.

To access the selected ListItem data, simply use AdapterView.AdapterContextMenuInfo. For example:.

 @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo menuinfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); long selectid = menuinfo.id; //_id from database in this case int selectpos = menuinfo.position; //position in the adapter switch (item.getItemId()) { case 1: doSomething(selectid); break; case 2: doSomethingElse(selectpos); } return super.onContextItemSelected(item); } 
0
Feb 05 '14 at 13:48
source share

A quick note for those still struggling, there are two methods

 registerForContextMenu(list); unregisterForContextMenu(list); 

Make sure you choose the first one.

0
Jun 05 '19 at 13:40
source share



All Articles