Hi, I'm trying to repeat (loop) the same action as long as the ImageView is pressed (pressed), but I can't, how do I do it? - The idea is to advance an arduino car through GET requests, once it is released ('depressed') the loop ends.
fooImageView = ...;
fooImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
int contador = 0;
while (fooImageView.isPressed()) {
System.out.printf("contador: %d", (++contador));
}
You can do this by detecting the events
ACTION_DOWN
andACTION_UP
and performing the desired action when they occur.Update:
What you want can be done by using a thread and by listening for events to start or stop it.
This will be the class used which, when the thread is running, can create an infinite loop, which will be stopped when you lift the key, which in turn indicates the thread to stop. I add a sleep to avoid blocking the application, you can reduce the amount of milliseconds between each call.