I want to open a dialog whenever a button is getting held down.
This is what I have tried:
// An alter box for a long pressed action
final Handler handler = new Handler();
Runnable mLongPressed = () -> {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Message");
builder.setMessage(text);
builder.show();
};
btn.setOnTouchListener(new OnSwipeTouchListener(getContext()) {
// Holding a button to show up a message
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
handler.postDelayed(mLongPressed, ViewConfiguration.getLongPressTimeout());
}
if ((motionEvent.getAction() == MotionEvent.ACTION_MOVE) || (motionEvent.getAction() == MotionEvent.ACTION_UP)) {
handler.removeCallbacks(mLongPressed);
}
return super.onTouch(view, motionEvent);
}
This works fine on my emulator, but not on my actual phone device. Why?