0

I'm a beginner on Android Studio and I've searched almost everywhere but haven't found anything working for me.

I want to update the view immediately after changing its background color every time in an infinite loop. I have tried with invalidate() but, after tapping, the view is still white. How can I redraw the view after changing?

public class Main extends AppCompatActivity {
    static int r = 0, g = 0, b = 0;
    static int current, direction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final View view = findViewById(R.id.textView);
        final View root = view.getRootView();

        view.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                while(true) {
                    rainBow();
                    root.setBackgroundColor(Color.rgb(r, g, b));
                    view.invalidate();
                    root.invalidate();
                }
            }
        });
    }

    public static void rainBow() {
        if(r == 0 && g == 0 && b == 0) {
            current = 1;
            direction = 1;
        } else if(r == 255 && g == 0 && b == 0) {
            current = 2;
            direction = 1;
        } else if(r == 255 && g == 255 && b == 0) {
            current = 1;
            direction = -1;
        } else if(r == 0 && g == 255 && b == 0) {
            current = 3;
            direction = 1;
        } else if(r == 0 && g == 255 && b == 255) {
            current = 2;
            direction = -1;
        } else if(r == 0 && g == 0 && b == 255) {
            current = 1;
            direction = 1;
        } else if(r == 255 && g == 0 && b == 255) {
            current = 3;
            direction = -1;
        }

        switch(current) {
            case 1:
                r += direction;
                break;

            case 2:
                g += direction;
                break;

            case 3:
                b += direction;
                break;
        }
    }
}
  • Android is propably blocking your code for two reasons. 1) Your loop never exists, thus blocking the UI Thread. It's forbidden. 2) Maybe you loop way to fast for the view to have the time to be updated. – vincrichaud Mar 28 '19 at 15:57
  • I've just tried to add a Thread.sleep(1000); but it's still not working –  Mar 28 '19 at 16:05
  • Note that you change the root background color and not the view color. Plus, you should not make the UI Thread sleep. Clicking your button yous launch a background thread, that regularly (like every second) change the color. – vincrichaud Mar 28 '19 at 16:08
  • I have tried to do it once and it worked, but in the loop it doesn't start a new thread, it just updates the background (I think) without showing it. Anyways I have just tried to do the loop in a new thread and when you tap the screen it turns black for a millisecond and then crashes... –  Mar 28 '19 at 16:12
  • Why would the loop start a new thread ? – vincrichaud Mar 28 '19 at 16:17
  • You said that "Clicking your button launch a background thread..." –  Mar 28 '19 at 16:20
  • Oh sorry, typo mistakes, I mean You should make it start a new Thread. Currently it doesn't – vincrichaud Mar 28 '19 at 16:21
  • I have tried to do it and it works for like a millisecond, so the display turns black but after, it crashes –  Mar 28 '19 at 16:26
  • You should use `runOnUIThread()` to update your view – vincrichaud Mar 28 '19 at 16:27
  • I have tried like here https://stackoverflow.com/questions/11140285/how-do-we-use-runonuithread-in-android but still not working :( –  Mar 28 '19 at 16:36
  • 1
    Thank you!!!!!! Now it works!! https://pastebin.com/xTyhs5S1 –  Mar 28 '19 at 17:03

0 Answers0