to change background color to red after 1 min of timer in android

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

            }

        };

Answers


Even simpler

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

},TimeUnit.MINUTES.toMillis(1));

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);
           }
    });
}

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


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


Need Your Help

The customization cannot be run because the solution ID in the document does not match

visual-studio-2010 vsto

I have developed a VSTO solution for Excel 2010 in VS 2010. The target framework is .NET 3.5. The solution works fine if I have all the components (dll, vsto and manifest and the Excel file) in one