androidandroid-studioandroid-layout

Changing Android UI at fixed intervals of time


The main motive is very basic: pause the loop makes the UI change and pause again makes the UI change to dynamically created View(s) and manually assigned id(s) to views.

Thread.sleep() is not working

for(int i=0;i<n;i++){
Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    @SuppressLint("ResourceType") Button mButton = (Button) findViewById(i);
                                    mButton.setBackgroundColor(Color.parseColor("#ABCDEF"));
                                }
                            }, 1000);
}

Using Handler is also not working. When Handler is put inside a loop, it takes the last value of loop always i.e. n-1 always.


Solution

  • 
        Handler handler = new Handler(Looper.myLooper());
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                        /**
                         * Change UI here
                         */
                        }
                    }, timeInMillisecons);