SilverBlue SilverBlue - 3 months ago 6
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(R.id.imgBtnAvatarback);

avatarScroll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ursprungAvatar = !ursprungAvatar;
if(ursprungAvatar) {
//image change with arrow
ImageButton avatarScroll = (ImageButton) findViewById(R.id.imgBtnAvatarback);
avatarScroll.setImageResource(R.drawable.avatarback_pfeil);
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
//
}
public void onFinish() {
//image change without arrow
ImageButton avatarScroll = (ImageButton) findViewById(R.id.imgBtnAvatarback);
avatarScroll.setImageResource(R.drawable.avatarback);
ursprungAvatar = !ursprungAvatar;
}
}.start();

} else {
//image change without arrow
ImageButton avatarScroll_ursprung = (ImageButton) findViewById(R.id.imgBtnAvatarback);
avatarScroll_ursprung.setImageResource(R.drawable.avatarback);
}

}
});

Answer

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).