android maps: How to Long Click a Map?

I’ve found an even easier way. Just make an overlay as the first overlay in the list that does not draw anything and use it to recognize gestures using the GestureDetector. It should then return true if it handled the event so it doesn’t get propagated. List<Overlay> overlays = mapView.getOverlays(); overlays.clear(); overlays.add(new MapGestureDetectorOverlay(new MyOnGestureListener())); And … Read more

How do you implement context menu in a ListActivity on Android?

On the onCreate method call registerForContextMenu like this: registerForContextMenu(getListView()); and then populate the menu on onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo). The menuInfo argument can provide information about which item was long-clicked in this way: AdapterView.AdapterContextMenuInfo info; try { info = (AdapterView.AdapterContextMenuInfo) menuInfo; } catch (ClassCastException e) { Log.e(TAG, “bad menuInfo”, e); return; } long … Read more

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

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) { … Read more

Detecting a long press in Android

GestureDetector is the best solution. Here is an interesting alternative. In onTouchEvent on every ACTION_DOWN schedule a Runnable to run in 1 second. On every ACTION_UP or ACTION_MOVE, cancel scheduled Runnable. If cancelation happens less than 1s from ACTION_DOWN event, Runnable won’t run. final Handler handler = new Handler(); Runnable mLongPressed = new Runnable() { … Read more