-2

Maybe someone can tell me what I'm doing wrong I'm writing an Android app that will show 3 views separates by a time interval so the basic idea is;

    ViewOne.setVisibility(View.INVISIBLE); //This is redundant but I put here for clarity
    ViewTwo.setVisibility(View.INVISIBLE);
    ViewThree.setVisibility(View.INVISIBLE);

    ViewOne.setVisibility(View.VISIBLE);
    //This supposed to make a 1 second stop.
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    ViewTwo.setVisibility(View.VISIBLE);
    //This supposed to make another 1 second stop.
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    ViewThree.setVisibility(View.VISIBLE);

but instead of making individual stops of 1000 milliseconds the activity is waiting 2000 to start and then show all the views at the same time. I'm new in android and java development guys sorry if I'm doing a stupid thing. Thanks in advance to all of you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Root cause: Your code blocked the UI thread, so it will not update UI until the thread are free (or not blocked).

Solution: I give you this solution to do what you want

final Handler handler = new Handler();

ViewOne.setVisibility(View.INVISIBLE); //This is redundant but I put here for clarity
ViewTwo.setVisibility(View.INVISIBLE);
ViewThree.setVisibility(View.INVISIBLE);

ViewOne.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        ViewTwo.setVisibility(View.VISIBLE);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                ViewThree.setVisibility(View.VISIBLE);
            }
        }, 1000);
    }
}, 1000);

Make sure you add final keyword before ViewTwo and ViewThree variable.

Update: I don't know your logic code behind or your intended, but if you want to repeat N time.

int secondsForEachStep = 3;
for (int i = 0; i < 3; i++) {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            wordText.setVisibility(View.INVISIBLE);
            myImageView.setVisibility(View.INVISIBLE);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    wordText.setVisibility(View.VISIBLE);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            myImageView.setVisibility(View.VISIBLE);
                        }
                    }, 1000);
                }
            }, 1000);

        }
    }, (i * secondsForEachStep + 1) * 1000);
}

Explain above code:

i = 0: After 1 seconds hide 2 views, after 2 seconds show view one, after 3 seconds show view 2.
i = 1: After 4 seconds hide 2 views, after 5 seconds show view one, after 6 seconds show view 2.
i = 2: After 7 seconds hide 2 views, after 8 seconds show view one, after 9 seconds show view 2.

For each step we need 3 seconds to finish, from that we can find the rule is

i * secondsForEachStep + 1
Son Truong
  • 13,661
  • 5
  • 32
  • 58