I have implemented button’s onLongClickListener in Android Dialog which auto-decreases number using timers. Here is the code
myIncreaseTimer = new Timer();
final Runnable runnable_decrease = new Runnable() {
public void run() {
int txt = Integer.parseInt(length.getText().toString());
txt -= 1;
length.setText(String.valueOf(txt));
if (txt == 0) {
myIncreaseTimer.cancel();
myIncreaseTimer.purge();
myIncreaseTimer = new Timer();
}
}
};
btnMinus.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
myIncreaseTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(runnable_decrease);
}
}, 0, 300);
return true;
}
});
When I am inside of activity, not dialog, I can stop the decrease via onTouchEvent when a user touches the screen
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
myIncreaseTimer.cancel();
myIncreaseTimer.purge();
myIncreaseTimer = new Timer();
}
return true;
}
However, I cannot do this in the dialog. Even more, the dialog does not have onTouchEvent.
Does anyone know how can I either implement onTouchEvent in Dialog or where should I stop the increase timer in the Dialog?
You can’t handle onTouch on Dialog so
Create a Activity, implement onTouchListener on it,
and in manifest file
So, now your activity is look like a DialogBox,