user3968848
user3968848

Reputation: 119

Android: Adding a delay to the display of toast?

I wish to display a toast if a certain condition is true or false. However I want this toast to delay for two seconds before it is displayed.

How can I do this?

Current if statement:

if (result.equals("true")) {

                        loginDataBaseAdapter.updateUploadedRecord(sessionId);

                            Toast.makeText(MathsGameResults.this,
                                    "Data is successfully uploaded.",
                                    Toast.LENGTH_LONG).show();

                        } else {
                            Toast.makeText(
                                    MathsGameResults.this,
                                    "Error while uploading. Please try again later.",
                                    Toast.LENGTH_LONG).show();
                        }

                    }

Upvotes: 0

Views: 7309

Answers (2)

Confuse
Confuse

Reputation: 5786

Delaying the code might not always work successfully especially on devices with a low RAM. Here is what you can do.

Define this variable

boolean result;

Add this code at end of loginDataBaseAdapter code if the data is successfully uploaded

result = true;
showResult(result);

Then add this method -

public void showResult(boolean i){

    if (result == true;) {

        loginDataBaseAdapter.updateUploadedRecord(sessionId);

        Toast.makeText(
                          MathsGameResults.this,
                          "Data is successfully uploaded.",
                          Toast.LENGTH_LONG
                      ).show();

    } else {

        Toast.makeText(
                       MathsGameResults.this,
                       "Error while uploading. Please try again later.",
                       Toast.LENGTH_LONG
                      ).show();
    }         
}

Anyways here is how to delay the code -

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            if (result == true;) {

                loginDataBaseAdapter.updateUploadedRecord(sessionId);

                Toast.makeText(
                                MathsGameResults.this,
                                "Data is successfully uploaded.",
                                Toast.LENGTH_LONG
                              ).show();

            } else {

                Toast.makeText(
                               MathsGameResults.this,
                               "Error while uploading. Please try again later.",
                               Toast.LENGTH_LONG
                              ).show();
           }

        }
}, 5000);

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

Use Handler

            // Handler which will run after 2 seconds.
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                      Toast.makeText(MathsGameResults.this,
                                "Data is successfully uploaded.",
                                Toast.LENGTH_LONG).show();
                }
            }, 2000);

Upvotes: 9

Related Questions