Skip to content

Instantly share code, notes, and snippets.

@ilguzin
Created November 29, 2012 13:30
Show Gist options
  • Save ilguzin/4169086 to your computer and use it in GitHub Desktop.
Save ilguzin/4169086 to your computer and use it in GitHub Desktop.
Simple way of making long tap event work in MapView. ShowMapActivity class
public class ShowMapActivity extends MapActivity {
...
private long touchActionDownStartTime;
private float histX;
private float histY;
...
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.maps_test); // bind the layout to the activity
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
...
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int actionType = ev.getAction();
/**
* touchActionDownStartTime > 0 --- check whether user released his finger
* ev.getEventTime() - touchActionDownStartTime > 500 --- check whether user
* pressed the screen as long as we need for registering of the "long tap" event
*/
if (touchActionDownStartTime > 0 &&
ev.getEventTime() - touchActionDownStartTime > 500) {
Toast.makeText(this, "Long push", 1000).show();
/**
* Reset start time to cancel subsequent event handling until user releases
* screen with his finger
*/
touchActionDownStartTime = 0;
// "On long tap" routines goes here!
}
switch (actionType) {
case MotionEvent.ACTION_DOWN:
// Setting the start of long tapping when user presses the screen
touchActionDownStartTime = ev.getEventTime();
histX = ev.getX(ev.getPointerCount()-1);
histY = ev.getY(ev.getPointerCount()-1);
break;
case MotionEvent.ACTION_UP:
/**
* Reset start time - this may stop event handling if user
* were pressing the screen not as long as we need for
* registering "long tap" event
*/
touchActionDownStartTime = 0;
break;
case MotionEvent.ACTION_MOVE:
float histXnew = ev.getX(ev.getPointerCount()-1);
float histYnew = ev.getY(ev.getPointerCount()-1);
if (histXnew-histX > 5 || histYnew-histY > 5)
touchActionDownStartTime = 0;
break;
}
return super.dispatchTouchEvent(ev);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment