-2

I have created a timer that change my layout color every 5 seconds,but there is an error coming mentioned in title .

I have tried using , but nothing seems to work

Handler refresh = new Handler(Looper.getMainLooper());
    refresh.post(new Runnable() {
        public void run()
        {
            colorchange();
        }
    });    
    private void colorchange() {
        final Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                rl = findViewById(R.id.am);
                int[] androidColors = getResources().getIntArray(R.array.androidcolors);
                int randomcolor = androidColors[new Random().nextInt(androidColors.length)];
                rl.setBackgroundColor(randomcolor);

            }

        },0,5000);
    }
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
arya
  • 7
  • 2
  • 1
    I sincerely find it an awful move to downvote an answer without providing a reason why (not just an opinion of liking or disliking it, but why it is not suitable). I especially like @VictorR.Oliveira's answer and how well it is structured. I know mine achieves the purpose as was asked, but I do not see why his was downvoted. I will thus give it an upvote to encourage people to say why an answer is not suitable. – Vinyl Jun 16 '19 at 14:13

2 Answers2

0

You can try to do it this way:


private void colorchange() {
        final Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                MyActivityName.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                         rl = findViewById(R.id.am);
                        int[] androidColors = getResources().getIntArray(R.array.androidcolors);
                        int randomcolor = androidColors[new Random().nextInt(androidColors.length)];
                        rl.setBackgroundColor(randomcolor);
                    }
                });

            }

        },0,5000);
    }

You have to run this on the main UI thread as shown above. Have a look at this similar post.

Vinyl
  • 333
  • 1
  • 7
0

I don't like the "run-run" thing, but that could only be avoided with Kotlin. Nevertheless, you should try to not overload your UI Thread with stuff that might freezes it and only rely to change inside what you really need. This means, things such as generating random int color should be outside of runOnUiThread.

package com.example.proofofconcept;

import android.os.Bundle;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;

import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ThreadLocalRandom;

public class BackgroundColorChanger extends AppCompatActivity {

    private RelativeLayout relativeLayout;

    private final int[] colors = {R.color.white,
            R.color.colorPrimary,
            R.color.colorPrimaryDark,
            R.color.yellow};

    private final TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            final int random = ThreadLocalRandom.current().nextInt(0, colors.length);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    relativeLayout.setBackgroundResource(colors[random]);
                }
            });
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_background_color_changer);
        findViews();
        colorChanger();
    }

    private void findViews() {
        relativeLayout = findViewById(R.id.colorfulLayout);
    }

    private void colorChanger() {
        final Timer timer = new Timer();
        timer.scheduleAtFixedRate(timerTask, 0, 1000);
    }
}
Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77