[TinyTut]ContextMenu for ListView

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

[TinyTut]ContextMenu for ListView

Postby MrSnowflake » Mon Dec 15, 2008 7:31 pm

Hi guys,

a lot of you want to know how to make a context menu for a ListView. PlusMinus alread did such a tutorial, but his version is only for SDKs prior to v1.0. So I'll make a new one you all can use.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. class MyActivity extends Activity {
  3.  
  4. protected static final int CONTEXT_CLICK_ME = Menu.FIRST;
  5.  
  6. public void onCreate(Bundle savedInstanceState){
  7.  
  8.   super(savedInstanceState);
  9.  
  10.   setContentView(R.layout.main);
  11.  
  12.   listView = (ListView)findViewById(R.id.listView);
  13.  
  14.   // Some cursor adapter
  15.  
  16.   Cursor c = this.managedQuery(uri, null, null, null, null);
  17.  
  18.   SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, c);
  19.  
  20.   mList.setAdapter(listAdapter );
  21.  
  22.  
  23.  
  24.   // This will make the listView create a ContextMenu when you long press it.
  25.  
  26.   // It also create the awsome menu option "Click Me"
  27.  
  28.   listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
  29.  
  30.     @Override
  31.  
  32.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
  33.  
  34.       menu.add(0, CONTEXT_CLICK_ME, 0, "Click Me");
  35.  
  36.     }
  37.  
  38.   });
  39.  
  40. }
  41.  
  42. public boolean onContextItemSelected(MenuItem item) {
  43.  
  44.   switch (item.getItemId()) {
  45.  
  46.   case CONTEXT_CLICK_ME:
  47.  
  48.     // This is actually where the magic happens.
  49.  
  50.     // As we use an adapter view (which the ListView is)
  51.  
  52.     // We can cast item.getMenuInfo() to AdapterContextMenuInfo
  53.  
  54.     AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
  55.  
  56.  
  57.  
  58.     // To get the id of the clicked item in the list use menuInfo.id
  59.  
  60.     Log.d(TAG, "list pos:"+menuInfo.position+" id:"+menuInfo.id);
  61.  
  62.     break;
  63.  
  64.   default:
  65.  
  66.     return super.onContextItemSelected(item);
  67.  
  68.   }
  69.  
  70.   return true;
  71.  
  72. }
  73.  
  74. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
I hope you guys find this usefull.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby chaoz1336 » Mon Jan 19, 2009 4:23 pm

thx a lot for this tut, kudos 2 u! :D
chaoz1336
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Wed Dec 10, 2008 5:19 pm

Postby mccarthycw » Fri Jul 17, 2009 5:46 pm

I have tried this example many times prior and my CreateContextMenuListener never gets called.
mccarthycw
Freshman
Freshman
 
Posts: 4
Joined: Fri Jul 17, 2009 5:40 pm
Location: Mechanicsburg, Pennsylvania, USA

Postby TheChosen » Sat Jul 18, 2009 6:18 pm

I think you have to call
Code: Select all
registerForContextMenu(getListView());
at the end of your onCreate method. At least this is what they do in the Notepad tutorial.
TheChosen
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Tue Jan 06, 2009 7:18 pm
Location: Germany

maybe listView

Postby Dayn » Fri Jul 31, 2009 3:34 pm

Hi !!

I thought listView is like my screen attachment.

Is it true? and is the java code run something like that ? because I have an issuewith my TextView

maybe I'm wrong to use this code.

could you help me

if it's true could you diplay me the xml code please about an example listView?

So I just wnat to make this screen and the ApiDemos code is too complex for me. I can't understand all of it.

my code is like that:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package m.dany.android.testandroid;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.database.Cursor;
  9.  
  10. import android.net.Uri;
  11.  
  12. import android.os.Bundle;
  13.  
  14. import android.util.Log;
  15.  
  16. import android.view.ContextMenu;
  17.  
  18. import android.view.Menu;
  19.  
  20. import android.view.MenuItem;
  21.  
  22. import android.view.View;
  23.  
  24. import android.view.View.OnCreateContextMenuListener;
  25.  
  26. import android.widget.AdapterView;
  27.  
  28. import android.widget.ListView;
  29.  
  30. import android.widget.SimpleCursorAdapter;
  31.  
  32.  
  33.  
  34. public class MyActivity extends Activity {
  35.  
  36.        
  37.  
  38.         protected static final int CONTEXT_CLICK_ME = Menu.FIRST;
  39.  
  40.         private static final String TAG = null;
  41.  
  42.        
  43.  
  44.         public void onCreate(Bundle savedInstanceState){
  45.  
  46.           super.onCreate(savedInstanceState);
  47.  
  48.           setContentView(R.layout.main);
  49.  
  50.           ListView listView = (ListView)findViewById(R.id.annonce);
  51.  
  52.           Uri uri = null;
  53.  
  54.         // Some cursor adapter
  55.  
  56.           Cursor c = this.managedQuery(uri, null, null, null, null);
  57.  
  58.           SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, 0, c, null, null);
  59.  
  60.          
  61.  
  62.           listView.setAdapter(listAdapter );
  63.  
  64.  
  65.  
  66.           // This will make the listView create a ContextMenu when you long press it.
  67.  
  68.           // It also create the awsome menu option "Click Me"
  69.  
  70.           listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
  71.  
  72.             @Override
  73.  
  74.             public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
  75.  
  76.               menu.add(0, CONTEXT_CLICK_ME, 0, "Click Me");
  77.  
  78.             }
  79.  
  80.           });
  81.  
  82.         }
  83.  
  84.         public boolean onContextItemSelected(MenuItem item) {
  85.  
  86.           switch (item.getItemId()) {
  87.  
  88.           case CONTEXT_CLICK_ME:
  89.  
  90.             // This is actually where the magic happens.
  91.  
  92.             // As we use an adapter view (which the ListView is)
  93.  
  94.             // We can cast item.getMenuInfo() to AdapterContextMenuInfo
  95.  
  96.             AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
  97.  
  98.  
  99.  
  100.             // To get the id of the clicked item in the list use menuInfo.id
  101.  
  102.             Log.d(TAG, "list pos:"+menuInfo.position+" id:"+menuInfo.id);
  103.  
  104.             break;
  105.  
  106.           default:
  107.  
  108.             return super.onContextItemSelected(item);
  109.  
  110.           }
  111.  
  112.           return true;
  113.  
  114.         }
  115.  
  116. }
  117.  
  118.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


and

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent">
  11.  
  12.    
  13.  
  14.     <ListView
  15.  
  16.         android:id="@android:id/list"
  17.  
  18.        android:layout_width="wrap_content"
  19.  
  20.        android:layout_height="wrap_content"/>
  21.  
  22.                
  23.  
  24.                 <TextView
  25.  
  26.                         android:id="@+id/annonce"
  27.  
  28.                 android:layout_width="wrap_content"
  29.  
  30.                 android:layout_height="wrap_content"
  31.  
  32.                 android:text="annonce"/>
  33.  
  34.  
  35.  
  36. </LinearLayout>
  37.  
  38.  
  39.  
  40.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Maybe I made a big novice mistake

thanks in advance

Dayn
Attachments
maybelistView.JPG
maybelistView.JPG (21.75 KiB) Viewed 11044 times
User avatar
Dayn
Developer
Developer
 
Posts: 31
Joined: Thu Jul 09, 2009 10:45 am
Location: Paris and Dijon

Re: [TinyTut]ContextMenu for ListView

Postby james.ingham » Fri Jan 07, 2011 3:35 pm

Is there any way to show the context menu by clicking the listview item rather than holding (long click)?
james.ingham
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 25, 2010 4:43 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests