How To Determine A Long Touch On Android?
Solution 1:
This is how do you normally create an onLongClickListener. Try this:
mapView.setOnLongClickListener(newOnLongClickListener() {
@OverridepublicbooleanonLongClick(View arg0) {
Toast.makeText(mapView.getContext(), "Hello 123", 2000);
returnfalse;
}
});
Reference to your edit:
This might be the way to get what you want.
privatefinalHandlerhandler=newHandler();
privatefinalRunnablerunnable=newRunnable() {
publicvoidrun() {
checkGlobalVariable();
}
};
// Other init stuff etc...@OverridepublicvoidonTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Execute your Runnable after 1000 milliseconds = 1 second.
handler.postDelayed(runnable, 1000);
mBooleanIsPressed = true;
}
if(event.getAction() == MotionEvent.ACTION_UP) {
if(mBooleanIsPressed) {
mBooleanIsPressed = false;
handler.removeCallbacks(runnable);
}
}
}
And now you can check with checkGlobalVariable function:
if(mBooleanIsPressed == true)
This is how you can handle this case. Good luck.
Solution 2:
Your are probably looking for a normal long click? You will have to set your view to be long clickable by adding android:longClickable to your views xml, or by calling setLongClickable(true). Then you can add an OnLongClickListener to the view. I dont know of a way to determine exactly how long the long click is. But the default long click is the same as the google maps long click that you mentioned.
Solution 3:
You can set up a longClickListener and a touchListener. Add a boolean class data member variable longClicked and set it to false initially. This is how you can set the longClickListener.
view.setOnLongClickListener(newView.OnLongClickListener() {
@OverridepublicbooleanonLongClick(View v) {
longClicked = true;
returnfalse;
}
});
For touchListener
view.setOnTouchListener(newView.OnTouchListener() {
@OverridepublicbooleanonTouch(View v, MotionEvent event) {
if(longClicked){
//Do whatever you want here!!
longClicked = false;
}
returnfalse;
}
});
It will give you the same effect as google maps long click. Hope it helps.
Solution 4:
use System.currentTimeMillis()
instead of ev.getEventTime();
Solution 5:
When MotionEvent.ACTION_UP
, endTime
will be set to ev.getEventTime(), this make setting endTime
to zero when MotionEvent.ACTION_MOVE
be not affect.
Instead of setting endTime
to zero, you should set startTime
to ev.getEventTime()
Post a Comment for "How To Determine A Long Touch On Android?"