A
A
Aleks_newsky2014-10-21 16:53:46
Android
Aleks_newsky, 2014-10-21 16:53:46

How to make "constantly" changing background color in android studio on button click?

Actually, here is the button code that starts changing our background to a random color

public void onRdmButtonClick(View view) {
        int x=0;
        Random rand = new Random();
        while(x<5) {
            mRelativeLayout.setBackgroundColor(-rand.nextInt(0xFFFFFF));
            x++;
        }
    }

Yes, the color changes to random, but only if you constantly poke yourself. The loop doesn't do it for me, for reasons I don't understand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2014-10-21
@Aleks_newsky

Well, I think that everything is changing for itself, maybe even randomly, but you just don’t have time to see it. There is no delay in the cycle. It will end in ten machine cycles, which is less than the human eye can see. The easiest option is to insert sleep. But since this is most likely your main UI thread, then you can’t do this in production - you need to create a separate thread so as not to freeze the interface thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question