creating a menu after a long click event on a list view creating a menu after a long click event on a list view android android

creating a menu after a long click event on a list view


First you need to register your context menu on list view.

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

Then, just override activity methods.

/** * MENU */@Overridepublic 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);      }}@Overridepublic 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 menu_list.xml file (you have to 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 it will help.


Instead of using onItemLongClick you can use

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

where you setup the options for edit and delete or whatever you need to.

The actions for the item selected from the context menu can be processed in

public boolean onContextItemSelected(final MenuItem item)

For more information on context menu see here.

For a step by step tutorial visit here.

EditThe second link is broken as it was quite old.But I guess you can refer one of the other highly voted answer to see all the steps involved,


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