I\'m new to Android so sorry if the question is easy to answer. I have two buttons, a decrease and an increase button, and in the middle of them a TextView which displays a
While the accepted answer is totally correct, it can be simplified a bit.
Basically, we can optimize two things:
So this is my version:
// global variables
Handler handler = new Handler();
Runnable runnable;
increaseView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
runnable = new Runnable() {
@Override
public void run() {
if (!increaseView.isPressed()) return;
increaseValue();
handler.postDelayed(runnable, DELAY);
}
};
handler.postDelayed(runnable, DELAY);
return true;
}
});
Here the runnable object is reused. And when the view is not pressed anymore, it will stop calling itself.
The decrease view or button can be defined in a similar way.