0

i am trying trying to change the background color to red after 1 min of timer and also i want to change background color to yellow after 1min 30 secs of timer in android. This is the code i am trying with, but its not working.

public void run() {
                timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
                updatedTime = timeSwapBuff + timeInMilliseconds;
                int secs = (int) (updatedTime / 1000);
                int mins = secs / 60;
                secs = secs % 60;
                int milliseconds = (int) (updatedTime % 1000);
                timerValue.setText("" + mins + ":"

                        + String.format("%02d", secs) + ":"

                        + String.format("%03d", milliseconds));
                 if(mins==1)
                {
                    View someView = findViewById(R.id.screen);
                    //View root = someView.getRootView();
                    someView.setBackgroundColor(Color.RED);
                }
                customHandler.postDelayed(this, 0);

            }

        };

5 Answers5

1

Even simpler

final View someView = findViewById(R.id.screen);
someView.postDelayed(new Runnable() {
   public void run() {
     someView.setBackgroundColor(Color.RED);
  }

},TimeUnit.MINUTES.toMillis(1));
aelimill
  • 1,015
  • 1
  • 10
  • 17
0

Try this:

private static final ScheduledExecutorService worker = 
  Executors.newSingleThreadScheduledExecutor();


Runnable task = new Runnable() {
    public void run() {
        View someView = findViewById(R.id.screen);
        someView.setBackgroundColor(Color.RED);
    }
};
worker.schedule(task, 60, TimeUnit.SECONDS);

Hope it helps.

From : https://stackoverflow.com/a/3072338/5778152

Community
  • 1
  • 1
Nicolas Cortell
  • 659
  • 4
  • 16
0

I think you should handle UI views in runOnUiThread.

if(mins==1)
{
    runOnUiThread(new Runnable() {
        @Override
           public void run() {
                View someView = findViewById(R.id.screen);
                //View root = someView.getRootView();
                someView.setBackgroundColor(Color.RED);
           }
    });
}
Kae10
  • 619
  • 2
  • 6
  • 17
0

I did something similar a few time ago. First of all into your layout (could be RelativeLayout or other):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
    android:id="@+id/screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    //…what you want

   </RelativeLayour>

Then in your activity:

    //global variables
    private long addedTime;
    private Handler handler;
    private RelativeLayout screen;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    screen = (RelativeLayout) findViewById(R.id.screen);

}

    private void start(){
        addedTime = 60;
        long changeTime = (Calendar.getInstance().getTimeInMillis()/1000) + addedTime;    
        handler = new Handler() {
            @SuppressWarnings("ResourceType")
            @Override
            public void handleMessage(Message msg) {
                if (addedTime == 60) {
                    screen.setBackgroundColor(Color.parseColor("#ff0000"));//red
                    addedTime = 120;
                }else{
                    screen.setBackgroundColor(Color.parseColor("#ffff00"));//yellow
                    addedTime = 60;

                }
                changeTime = changeTime + addedTime;//add time for the new color change
                handler.postDelayed(getRunnable(changeTime), 1000);

                super.handleMessage(msg);
            }
        };
        handler.postDelayed(getRunnable(changeTime), 1000);
    }

    private Runnable getRunnable(final long time) {
        return new Runnable() {
            @Override
            public void run() {
                Message message;
                Calendar calendar = Calendar.getInstance();
                if (calendar.getTimeInMillis() / 1000 == time) {
                    message = new Message();
                    handler.sendMessage(message);
                } else {
                    handler.postDelayed(getRunnable(time),1000);
                }
            }
        };
    }

The above example will made an infinite loop, so watch out. Hope to be usefull for you

jlggary
  • 41
  • 5
0

You can do this in RGB ways. If you want to change color softly during time you can also check this way.

 val countDownTimer = object : CountDownTimer(totalTime, 20) {
                override fun onFinish() {
                    
                }
    
                override fun onTick(millisUntilFinished: Long) {
                    val pr = (totalTime - millisUntilFinished).toFloat() / totalTime
                    if(pr <= 1f && pr>=0f){
                        binding.pbTimerRect.progress = pr
                            if ((pr * 255).toInt() <= 255) {
                                if(pr<=0.5) {
                                    binding.lytTimerBackGround.background.setTint(Color.rgb((pr*2* 255).toInt(), 255, 0))
                                } else {
                                    binding.lytTimerBackGround.background.setTint(Color.rgb(255,(255-((pr-0.5)*2* 255).toInt()), 0))
                                }
                            }
                        }
                    } else {
                        if(pr<0) {
                            onFinish()
                        }
                    }
                }
            }