Andrew1410
Andrew1410

Reputation: 21

Android Studio: Updating multiple buttons' backgrounds one by one when clicking a button

I am writing here because I have a very annoying problem in Android Studio. My "application" is extremely simple, one page with 13 button; All I want is simple: update the first twelve buttons one by one when I click the 13th button. I would like to see buttons updating with a little interval between each of them, but I can't understand how to do it. I tried many tricks inside the "onClick" method but I can not figure out how to solve it; what I obtain is that after some time (the time obtained adding up the various "sleep" I put in the function) all the buttons become colored at the same time. I put my last attempt, but if you have any other way to do that I am willing to change the way to proceed.

int[] buttonIDs = new int[] {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7,
        R.id.button8, R.id.button9, R.id.button10, R.id.button11, R.id.button12 };
int currentI = 0;

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

    final Button goButton = (Button) findViewById(R.id.button13);

    goButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (currentI < buttonIDs.length) {
                Button b = (Button) findViewById(buttonIDs[currentI]);
                b.setBackgroundColor(Color.parseColor("#FF22FF"));
                currentI++;

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Thread t = new Thread() {
                    public void run() {
                        goButton.performClick();
                    }
                };

                t.start();
            }
        }
    });

}

The result of this attempt is that the first button become colored and then I get "Application has stopped" error on the Android Emulator.

Thanks in advance

Upvotes: 0

Views: 238

Answers (2)

Akash Popat
Akash Popat

Reputation: 420

Use this

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 1000ms
  }
}, 1000);

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83527

The basic idea here is that you need to create a new thread which signals the UI thread to update each button. In between each update, the new thread needs to wait until the UI has a chance to perform the update. I'm not very experienced with this kind of thing. You can most likely do this with a Handler. There might be more high-level tools you can use instead, but I'm not sure.

You definitely do not want to sleep the UI thread. This will only cause the UI to become unresponsive.

Upvotes: 0

Related Questions