SilverBlue SilverBlue - 9 months ago 47
Android Question

How I can leave the counter code?

My Problem:
I don't know, how I can leave my counter-code (CountDownTimer), IF the user click the imageButton within the counter of 2 seconds

My Code:

final ImageButton avatarScroll = (ImageButton) findViewById(;

avatarScroll.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ursprungAvatar = !ursprungAvatar;
if(ursprungAvatar) {
//image change with arrow
ImageButton avatarScroll = (ImageButton) findViewById(;
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
public void onFinish() {
//image change without arrow
ImageButton avatarScroll = (ImageButton) findViewById(;
ursprungAvatar = !ursprungAvatar;

} else {
//image change without arrow
ImageButton avatarScroll_ursprung = (ImageButton) findViewById(;



If I get the behavour right, all you need to do, is adding timer.cancel() in the else case and keep a reference to the created timer (eg make it a field).