Young Fellow
Young Fellow

Reputation: 47

How do I add a delay before a command, without pausing the GUI - JAVA


I'm working on an app using android studio, and I need to delay in before the text changes, this is the part of code that needs a delay:

public void sleep(int time) {
    try {
 
        // Thread.sleep(time);        

        // TimeUnit.SECONDS.sleep(time);

        // Thread.wait(time);

    } catch (Exception e) {}
}


public void MoveOn() {
        UsAnswer = Integer.parseInt(Input.getText().toString());

        if (UsAnswer != cran) {

            feedback.setTextColor(Color.RED);

            feedback.setText("✖");

            // Wait 2 seconds

            sleep(2);

            feedback.setText("");

            Input.setText("");

        } else {

            feedback.setTextColor(Color.GREEN);

            feedback.setText("✔");

            // Wait 2 seconds

            sleep(2);

            feedback.setText("");

            MainLoop();
        }
    }

As you can see, in my sleep function (at least I think they're called functions in Java 😁) I used three different sleep methods. However, they all pause the whole GUI, I just need it to wait two seconds, showing the earlier action feedback.setText("✖"); & feedback.setText("✔");.

I read on this post that I should use a Swing Timer, but wasn't sure how. To be honest (being a beginner) I have no idea what a Swing Timer is 😁.

PS: If you do answer my question, then please don't take my code and modify it so that it will work perfectly, just give me the code and show me how to use it. That way I won't be lazy and just copy/paste, and also, that way I'll learn 😁.

Upvotes: 0

Views: 414

Answers (1)

AShX
AShX

Reputation: 412

To run with delay you can use Handler :

 public static void initAfterDelay(Runnable runnable,int delay){
        final Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(runnable, delay);
    }

Runnable is your void, and should be used with "this::", that you will shot that method situated in this class, or with the class name and delay in milliseconds, like 100,200 or else value, example:

///if void `petAllDogs` situated in the same class
  initAfterDelay(this::petAllDogs,250); 

  //if void `petAllDogs`situated in the other class, for example Utils
   initAfterDelay(Utils::petAllDogs,250); 

If you want to delay just some method in Activity/Fragment directly without setting up constructor you can next handler code:

public void initAfterDelay2 (){
        final Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(this::SomeFunction, 500);
    }

this can be replaced with SomeActivity::someFunction to access , but SomeMethod should be static and shouldn't have any parameters;

If you want to run void with constructor use the next code in any declared function

     private void some(){
    final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        voidWithParams(parameter 1, parameter 2);
                        Log.d("Handler", "Running Handler");
                    }
                }, 500);
}

or with lambda expression

private void some(){
        final Handler handler = new Handler();
        handler.postDelayed(() -> {
            voidWithParams(parameter 1, parameter 2);
            Log.d("Handler", "Running Handler");
        }, 1000);
    }

All provided code will have similar result - execution of method after delay

Upvotes: 1

Related Questions