Android OnLongClickListener not firing on MapView Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android OnLongClickListener not firing on MapView without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I just registered an OnLongClickListener on my my MapView on an Android app I’m currently writing. For some reason however the onLongClick event doesn’t fire.

Here’s what I’ve written so far:

public class FriendMapActivity extends MapActivity implements OnLongClickListener {
    private static final int CENTER_MAP = Menu.FIRST;
    private MapView mapView;
    private MapController mapController;
    //...
    private boolean doCenterMap = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friendmapview);
        this.mapView = (MapView) findViewById(R.id.map_view);
        this.mapController = mapView.getController();

        mapView.setBuiltInZoomControls(true);
        mapView.displayZoomControls(true);
        mapView.setLongClickable(true);
        mapView.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
                //NEVER FIRES!!
                return false;
            }
        });

        //...
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_3:
            mapController.zoomIn();
            break;
        case KeyEvent.KEYCODE_1:
            mapController.zoomOut();
            break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int actionType = ev.getAction();
        switch (actionType) {
        case MotionEvent.ACTION_MOVE:
            doCenterMap = false;
            break;
        }

        return super.dispatchTouchEvent(ev);
    }

        ...
}

May overlays which I’m adding cause the problem?? Any suggestions?

Answer

In the mean time I found the “solution” (or workaround, call it as you like) by myself. The way I worked through this issue is by using a GestureDetector and forwarding all touch events to that object by implementing an according OnGestureListener interface.

I’ve posted some code on my blog if anyone is interested: http://juristr.com/blog/2009/12/mapview-doesnt-fire-onlongclick-event/

Don’t ask me why this didn’t work by hooking up the OnLongClickListener directly on the MapView. If someone has an explanation let me know 🙂

UPDATE:
My previously suggested solution using a GestureDetector posed some drawbacks. So I updated the blog post on my site.

We are here to answer your question about Android OnLongClickListener not firing on MapView - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji