-1

I want to change colour of countdowntimer in a specific time. For example, After 50 sec, colour changes greeen to yellow.Could you help me please? Here, you can check my code. Till 50 sec ok.the colour is green. However, after 50th sec, app crashes.

       final CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) 
        { 

        public void onTick(long millisUntilFinished) {
            x = millisUntilFinished/1000;
            timer.setText( x +  "    remaining left ");
            if ( 50< x ){
                timer.setTextColor(Color.parseColor("#32e76b"));

            }
            else{
                timer.setTextColor(Color.parseColor("FFE9DD33"));

            }
K.Os
  • 5,123
  • 8
  • 40
  • 95
aig_88
  • 13
  • 7

3 Answers3

0

Try using like this

ContextCompat.getColor(context, R.color.your_color);

Check this

Simo
  • 345
  • 4
  • 12
0

Your representation of color is AARRGGBB but is should be #AARRGGBB, you missed # in your color so it will throw an IllegalArgumentException exception.

So change like this,

timer.setTextColor(Color.parseColor("#FFE9DD33"));
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
-1

Try create a colors.xml file inside values folder like that:

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <color name="green">#32e76b</color>

    <color name="yellow">yor_yellow_color_code</color>

    <color name="red">yor_red_color_code</color>

</resources>

And set like this:

final CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) 
        { 

        public void onTick(long millisUntilFinished) {
            x = millisUntilFinished/1000;
            timer.setText( x +  "    remaining left ");
            if ( 50< x ){
                timer.setTextColor(ContextCompat.getColor(your_context,R.color.green)}
            else{
                timer.setTextColor(ContextCompat.getColor(your_context,R.color.yellow);

            }
Bruno Ferreira
  • 1,561
  • 1
  • 10
  • 17