Creating a ContextMenu on a ListView

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby pedroteixeira07 » Wed May 05, 2010 11:07 am

Hi there! Thanks for the tutorial. Works perfectly!
As someone already mentioned before, is it possible to make this with a single click instead of long one ?

Thank you very much
pedroteixeira07
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Sun Oct 18, 2009 6:06 pm
Location: Funchal

Top

Postby padde » Wed May 05, 2010 12:11 pm

Just set an OnItemClickListener on the List and tell the clicked View to show their contextmenu.

For better understanding.. here is the code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.util.ArrayList;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.view.ContextMenu;
  11.  
  12. import android.view.MenuItem;
  13.  
  14. import android.view.View;
  15.  
  16. import android.view.ContextMenu.ContextMenuInfo;
  17.  
  18. import android.view.View.OnCreateContextMenuListener;
  19.  
  20. import android.widget.AdapterView;
  21.  
  22. import android.widget.ArrayAdapter;
  23.  
  24. import android.widget.ListView;
  25.  
  26. import android.widget.AdapterView.AdapterContextMenuInfo;
  27.  
  28. import android.widget.AdapterView.OnItemClickListener;
  29.  
  30.  
  31.  
  32. public class ContextMenuSample extends Activity {
  33.  
  34.     protected static final int CONTEXTMENU_DELETEITEM = 0;
  35.  
  36.  
  37.  
  38.     protected ListView mFavList;
  39.  
  40.     protected ArrayList<Favorite> fakeFavs = new ArrayList<Favorite>();
  41.  
  42.  
  43.  
  44.     public void onCreate(Bundle icicle) {
  45.  
  46.         super.onCreate(icicle);
  47.  
  48.         setContentView(R.layout.main);
  49.  
  50.  
  51.  
  52.         fakeFavs.add(new Favorite("John", "nice guy"));
  53.  
  54.         fakeFavs.add(new Favorite("Yasmin", "hot girl"));
  55.  
  56.         fakeFavs.add(new Favorite("Jack", "cool guy"));
  57.  
  58.  
  59.  
  60.         this.mFavList = (ListView) this.findViewById(R.id.list_favorites);
  61.  
  62.        
  63.  
  64.         // this triggers the ContextMenu of an Item in the List with just a Click
  65.  
  66.         mFavList.setOnItemClickListener(new OnItemClickListener() {
  67.  
  68.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  69.  
  70.                 arg1.showContextMenu();
  71.  
  72.             }
  73.  
  74.         });
  75.  
  76.        
  77.  
  78.         initListView();
  79.  
  80.     }
  81.  
  82.  
  83.  
  84.     private void refreshFavListItems() {
  85.  
  86.         mFavList.setAdapter(new ArrayAdapter<Favorite>(this, android.R.layout.simple_list_item_1, fakeFavs));
  87.  
  88.     }
  89.  
  90.  
  91.  
  92.     private void initListView() {
  93.  
  94.         refreshFavListItems();
  95.  
  96.         mFavList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
  97.  
  98.              public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  99.  
  100.                  menu.setHeaderTitle("ContextMenu");
  101.  
  102.                  menu.add(0, CONTEXTMENU_DELETEITEM, 1, "Delete this favorite!");
  103.  
  104.             }
  105.  
  106.         });
  107.  
  108.     }
  109.  
  110.  
  111.  
  112.     public boolean onContextItemSelected(MenuItem aItem) {
  113.  
  114.         AdapterContextMenuInfo info = (AdapterContextMenuInfo) aItem.getMenuInfo();
  115.  
  116.        
  117.  
  118.         switch (aItem.getItemId()) {
  119.  
  120.             case CONTEXTMENU_DELETEITEM:
  121.  
  122.                 Favorite favContexted = (Favorite) mFavList.getAdapter().getItem(info.position);
  123.  
  124.                 fakeFavs.remove(favContexted);
  125.  
  126.                 refreshFavListItems();
  127.  
  128.                 return true;
  129.  
  130.         }
  131.  
  132.         return false;
  133.  
  134.     }
  135.  
  136.  
  137.  
  138.     protected class Favorite {
  139.  
  140.         protected String name;
  141.  
  142.         protected String kindness;
  143.  
  144.  
  145.  
  146.         protected Favorite(String name, String kindness) {
  147.  
  148.             this.name = name;
  149.  
  150.             this.kindness = kindness;
  151.  
  152.         }
  153.  
  154.  
  155.  
  156.         public String toString() {
  157.  
  158.             return name + " (" + kindness + ")";
  159.  
  160.         }
  161.  
  162.  
  163.  
  164.         public boolean equals(Object o) {
  165.  
  166.             return o instanceof Favorite && ((Favorite) o).name.compareTo(name) == 0;
  167.  
  168.         }
  169.  
  170.     }
  171.  
  172. }
  173.  
  174.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


--- Note ---
I had to change some small things to make the code work with android 1.6 but nothing essential.


Greets Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby pedroteixeira07 » Wed May 05, 2010 3:33 pm

muchas gracias! It's running exactly as I wanted :D
pedroteixeira07
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Sun Oct 18, 2009 6:06 pm
Location: Funchal

Re: Creating a ContextMenu on a ListView

Postby karanjot.bhasin » Sat Dec 04, 2010 9:18 pm

how to delete a item in listview(created dynamically by sqlite)
when user clicks on a item in alist a context menu should be shown having a delete button?
i
karanjot.bhasin
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Dec 04, 2010 9:15 pm

Re: Creating a ContextMenu on a ListView

Postby TuxBobble » Sun Jan 16, 2011 11:03 pm

Is there a way of customizing the context menu per List View item?

So for instance, each of my list view items has a Status. If the status == true, I want the context menu to say "Edit". If the status == false, I want the context menu to say "Remove/Delete".

Is there a simple way of making this work? It seems to me that the context menu is created without reference to the specific item in the list. The only time I see an explicit item reference is when a context menu item is CHOSEN (which is after the display has already shown "Edit" or "Remove/Delete", for instance.

I don't see how to find the item of the ListView when onCreateContextMenu is called such that I can customize what context menu options are offered for the selected item.
TuxBobble
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Dec 30, 2009 7:36 pm

Re:

Postby Nichotin » Mon Mar 07, 2011 12:58 pm

shiteiru wrote:is there a way to open the context menu with just a single normal click?

right now you have to press an item long to get the contextmenu.


Yes, you can do that. When you have used setOnCreateContextMenuListener() like in this example, you can use the method openContextMenu() on that view to open the context menu instantly. openContextMenu() can for instance be called by the OnClickListener for a button. :)
Nichotin
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Mar 07, 2011 12:53 pm

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests