Context menu can't be created on MapView.

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Context menu can't be created on MapView.

Postby thl16888 » Fri Dec 26, 2008 2:54 pm

hi all
I want to create a context menu when LongClick is hitted. As
result mapview is opened successfully; but the LongClick got no
response. Would you like to give me a hand how to solve it? Below is
the complete code. Thank you very much.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3. package android.demo;
  4.  
  5. import com.google.android.maps.MapActivity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import com.google.android.maps.MapView;
  10.  
  11. import android.view.ContextMenu;
  12.  
  13. import android.view.View;
  14.  
  15. import android.util.*;
  16.  
  17. public class demo extends MapActivity {
  18.  
  19.     /** Called when the activity is first created. */
  20.  
  21.         MapView mapView;
  22.  
  23.     @Override
  24.  
  25.     public void onCreate(Bundle savedInstanceState) {
  26.  
  27.         super.onCreate(savedInstanceState);
  28.  
  29.         setContentView(R.layout.main);
  30.  
  31.         mapView = (MapView) findViewById(R.id.mapview1);
  32.  
  33.         // We also want to show context menu for longpressed items in
  34.  
  35. the gallery
  36.  
  37.       //init the map view
  38.  
  39.  
  40.  
  41.  
  42.  
  43.         mapView.setOnLongClickListener(new View.OnLongClickListener(){
  44.  
  45.  
  46.  
  47.  
  48.  
  49.         public boolean onLongClick(View v) {
  50.  
  51.         mapView.showContextMenu();
  52.  
  53.         return true;
  54.  
  55.         }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.         });
  62.  
  63.  
  64.  
  65.  
  66.  
  67.         mapView.setOnCreateContextMenuListener(new
  68.  
  69. View.OnCreateContextMenuListener() {
  70.  
  71.         public void onCreateContextMenu(ContextMenu menu, View v,
  72.  
  73. ContextMenu.ContextMenuInfo menuInfo) {
  74.  
  75.                   menu.setHeaderTitle("Menu");
  76.  
  77.               menu.add(0, 1, 0, "Add");
  78.  
  79.               menu.add(0, 2, 0, "Delete");
  80.  
  81.               menu.add(0, 3, 0, "Edit");
  82.  
  83.            }
  84.  
  85.         });
  86.  
  87.  
  88.  
  89.  
  90.  
  91.         registerForContextMenu(mapView);
  92.  
  93.  
  94.  
  95.  
  96.  
  97.     }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.     @Override
  104.  
  105.     protected boolean isRouteDisplayed() {
  106.  
  107.         // TODO Auto-generated method stub
  108.  
  109.         return false;
  110.  
  111.     }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
thl16888
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Nov 04, 2008 2:18 pm

Top

Postby joos » Fri Dec 26, 2008 11:13 pm

I am just working on the same thing:

This in onCreate works for me:

myPointOfInterestView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(0, 1, 0, "Open Detail page");
}
});
joos
Freshman
Freshman
 
Posts: 4
Joined: Thu May 22, 2008 6:59 pm

Postby thl16888 » Sat Dec 27, 2008 2:13 am

Thanks for your response. I can't still get it to work. Would you like to share your code with me ? Thank you very much.
thl16888
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Nov 04, 2008 2:18 pm

Postby joos » Fri Jan 02, 2009 9:57 am

in
public boolean dispatchTouchEvent(MotionEvent event) {
make sure you return true or
return super.dispatchTouchEvent(event);
such that the click event gets delegated.

Then you need


/**
* This is called after a long press
*/
public boolean onContextItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case 1:
// do something here
break;
// more case options here
default:
return super.onContextItemSelected(item);
}
return true;
}


That should work.
-- ph
joos
Freshman
Freshman
 
Posts: 4
Joined: Thu May 22, 2008 6:59 pm

Postby sendhilgb » Thu May 14, 2009 5:57 am

Hi
I am facing the same problem have you solved this issue .
sendhilgb
Freshman
Freshman
 
Posts: 6
Joined: Mon Apr 20, 2009 9:33 am

Postby serkan.dogantekin » Mon Jul 13, 2009 11:35 pm

I am one of the guys that cannot make the solutions declared above, so i define this (ugly) solution:

* define a handler in the ui activity
override its handleMessage method, so that when a specified message is received (for example:1) it will trigger activity's openContextMenu method.
* define an overlay and add to mapview
* in the overlay override onTouchEvent such that:
when action_down
take the touch points (Px, Py)
take timestamp (T)
trigger a thread (i will mention above it, called it as ThreadX)
when action_move
if touch points are in a defined range of Px and Py do nothing (in my example my range is 5 pixels)
else set T as -1
when other event modes
set T as -1
* in ThreadX (in its run method-my thread class implements Runnable)
sleep for 3 seconds
if T != -1 and the difference between current timestamp and T is greater or equal to 3 seconds, call the handler declared in the ui activity with the specified message

i know that this an ugly solution, but it works :)
serkan.dogantekin
Developer
Developer
 
Posts: 27
Joined: Thu Jun 04, 2009 6:12 pm
Location: Istanbul/Turkey

Top

Re: Context menu can't be created on MapView.

Postby raul.sh » Tue Jan 05, 2010 10:19 pm

Hi All -

Did anyone find the solution to this problem? I cant seem to get it work as well.

Thanks,

Raul

thl16888 wrote:hi all
I want to create a context menu when LongClick is hitted. As
result mapview is opened successfully; but the LongClick got no
response. Would you like to give me a hand how to solve it? Below is
the complete code. Thank you very much.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package android.demo;
  3. import com.google.android.maps.MapActivity;
  4. import android.os.Bundle;
  5. import com.google.android.maps.MapView;
  6. import android.view.ContextMenu;
  7. import android.view.View;
  8. import android.util.*;
  9. public class demo extends MapActivity {
  10.     /** Called when the activity is first created. */
  11.         MapView mapView;
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main);
  16.         mapView = (MapView) findViewById(R.id.mapview1);
  17.         // We also want to show context menu for longpressed items in
  18. the gallery
  19.       //init the map view
  20.  
  21.  
  22.         mapView.setOnLongClickListener(new View.OnLongClickListener(){
  23.  
  24.  
  25.         public boolean onLongClick(View v) {
  26.         mapView.showContextMenu();
  27.         return true;
  28.         }
  29.  
  30.  
  31.         });
  32.  
  33.  
  34.         mapView.setOnCreateContextMenuListener(new
  35. View.OnCreateContextMenuListener() {
  36.         public void onCreateContextMenu(ContextMenu menu, View v,
  37. ContextMenu.ContextMenuInfo menuInfo) {
  38.                   menu.setHeaderTitle("Menu");
  39.               menu.add(0, 1, 0, "Add");
  40.               menu.add(0, 2, 0, "Delete");
  41.               menu.add(0, 3, 0, "Edit");
  42.            }
  43.         });
  44.  
  45.  
  46.         registerForContextMenu(mapView);
  47.  
  48.  
  49.     }
  50.  
  51.  
  52.     @Override
  53.     protected boolean isRouteDisplayed() {
  54.         // TODO Auto-generated method stub
  55.         return false;
  56.     }
  57.  
  58.  
  59.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
raul.sh
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Nov 17, 2009 6:15 pm

Postby Sojan P R » Thu Feb 04, 2010 7:29 am

Hi,
I also had the same problem but I used "openContextMenu" routine to get around the clutter.
It seems that long click is not working with mapviews,So i used "On tap" event.
Steps are as follows
1. register your map view for context menu

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. yourMapviewObj.setLongClickable(true);
  2.  
  3.              registerForContextMenu(yourMapviewObj);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


2. Create your context menu(I have created it in xml and added it to resource,the infalted it using "Menu infaltor"
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  4.  
  5.     <item android:id="@+id/call"
  6.  
  7.          android:title="Call mobile" />
  8.  
  9.     <item android:id="@+id/sms"
  10.  
  11.          android:title="Send an sms" />
  12.  
  13.      <item android:id="@+id/proximity"
  14.  
  15.          android:title="Send proximity alert" />
  16.  
  17.       <item android:id="@+id/SOS"
  18.  
  19.          android:title="Send an SOS" />
  20.  
  21. </menu>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


3.You have to override the "OnCreatecontextmenu" and inflate the menu
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.   public void onCreateContextMenu(ContextMenu menu, View v,
  2.  
  3.                 ContextMenuInfo menuInfo) {
  4.  
  5.                 MenuInflater cntmenu = getMenuInflater();
  6.  
  7.                 cntmenu.inflate(R.layout.contextmenu, menu);
  8.  
  9.                 return ;
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


4.Finally "on tap" method youa can open your context menu
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public boolean onTap(GeoPoint p, MapView mapView) {
  2.  
  3.         openContextMenu(POIMapView);
  4.  
  5.          return super.onTap(p, mapView);
  6.  
  7. }
  8.  
  9.        
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


it seems working fine and serve the purpose of context menu linked with long press.You can see that on scrolling the map it will not fire the "on tap".

IS there any other way around ?
Sojan P.R
sojanpr@gmail.com
Trivandrum,India
Sojan P R
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Feb 04, 2010 7:11 am
Location: Trivandrum ,India

Re: Context menu can't be created on MapView.

Postby Ulathar » Thu Feb 24, 2011 4:44 am

hmm does not work for me (there is no onTap method i could override).

i have an osmdroid Mapview:

Code: Select all
@Override
    protected void onCreate(Bundle savedInstanceState) {
      
       [...]
               
       this.mapViewOSM.setLongClickable(true);
       this.registerForContextMenu(mapViewOSM);
   }



Code: Select all
@Override
    public void onCreateContextMenu(ContextMenu cm, View v, ContextMenuInfo menuInfo) {       
       super.onCreateContextMenu(cm, v, menuInfo);
       System.out.println("Yay i was called! ");
       cm.setHeaderTitle("Optionen: ");
       cm.add(0, v.getId(), 0, "Aktion 1"); 
       cm.add(0, v.getId(), 0, "Aktion 2");       
    }




but for some reason, the ContextMenu is NEVER called :(.

what i try to achieve:

when the user "long clicks" on the OSMMapView, a contextmenu should pop up for further userinteraction!
Ulathar
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sat Jun 05, 2010 9:55 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 2 guests