0

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 .

  • 1
    I didnt get what you are trying to do but if you want delay before executing next line of code, you can check this link https://stackoverflow.com/questions/1520887/how-to-pause-sleep-thread-or-process-in-android – Ferhat Ergün Jul 14 '21 at 05:49

1 Answers1

1

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

AShX
  • 358
  • 3
  • 14
  • Thanks for answering @GremlinShX, but is there a way of declaring a void statement, then just calling it for any command later, for example: `feedback.setText("✖"); sleep(2); feedback.setText("");` – Young Fellow Jul 14 '21 at 10:21
  • 1
    Also, when you said: _**Runnable is your void method** and delay in milliseconds, like 100,200 or else value_, but I can't add my void method, because it must be a runnable ! – Young Fellow Jul 14 '21 at 11:13
  • 1
    @YoungFellow thank you, I ran out of coffee when I wrote that. I fixed my answer and explained how to use it. – AShX Jul 14 '21 at 12:59
  • 1
    @YoungFellow you want to repeat your code constantly after some delay ? Because I am not sure that I understand what you want. – AShX Jul 14 '21 at 13:20
  • Thanks, @GremlinShX, now I understand. What I meant is that I wanted just a one liner piece of code that would do the job, and you added that to your answer. Again, thanks allot – Young Fellow Jul 15 '21 at 13:06