Skip to content
Advertisement

Change Color everysecond AndroidStudio

Hello again guys firstly i am new at android studio.i am trying to make countdowntimer and i made it its working

then i want to make change background color every tick , every second.

thank you!

JavaScript

Advertisement

Answer

The problem lies in the fact that on each tick you are setting the background color to white and then immediately to red, inside onTick. This will most likely result for the user to not be able to see the white color, because it is immediately overwritten on every tick. You need a way to change to a single color for each tick.

Don’t forget to always run UI related code on the UI thread. You can read more about on how to do it here.

In case you want to alternate between white color and red for every tick, then you can have a flag in your CountDownTimer like so:

JavaScript

Or better, you can rely on the value millisUntilFinished which is given as the single argument to onTick, like so:

JavaScript

This approach should be better because if I understand correctly from the documentation of CountDownTimer, it seems that the millisUntilFinished (given in onTick) is not guaranteed to be a multiple of the interval, which would depend also on the amount of work the onTick method is doing.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement