Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a list view connected to a database, showing a all the entries. I want a menu to show up if the user long clicks a item in the listview(database entry), showing options to edit or delete the entry. how can i do this.

Till now, I have tried using a onItemLongClick listener and a toast in it showing the id of the view long clicked.

@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; } 
like image 714
Eriz Avatar asked Jun 20 '13 07:06

Eriz


People also ask

Which menu will display after long press click on any view?

Context menu and contextual action mode A context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

What is onContextItemSelected?

onContextItemSelected() is called for all currently existing fragments starting with the first added one. In my case the fragments are used to show the content of a folder structure.


1 Answers

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  */  @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 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.

like image 169
Zhar Avatar answered Sep 28 '22 00:09

Zhar