Search code examples
javaandroidandroid-studiodelayprogressdialog

How can add a delay to Progress Dialog - Java - Android - Android Studio


I want to make a progress dialog appear for 2 or 3 seconds.
It won't actually do anything other than say progress.
I want to add a delay in progress bar. How can succeed this?
It appears at first but for a very short time.
My code is below:

     private void registerNewAccount(String username,String email, String password, String mobile, String gender){
            final ProgressDialog progressDialog = new ProgressDialog( RegisterActivity.this);
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(false);
            progressDialog.setTitle("Registering New Account");
            progressDialog.show();
            String uRl = "http://192.168.1.6/Projects/LoginRegisterAndroid/register.php";
            StringRequest request = new StringRequest(Request.Method.POST, uRl, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (response.equals("Successfull")){
                        progressDialog.dismiss();
                        Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                        finish();
                    }
                    else{
                        progressDialog.dismiss();
                        Toast.makeText(RegisterActivity.this, response, Toast.LENGTH_SHORT).show();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(RegisterActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
                }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String,String> param = new HashMap<>();
                    param.put("username", username);
                    param.put("email", email);
                    param.put("psw", password);
                    param.put("mobile", mobile);
                    param.put("gender", gender);
                    return param;
                }
            };
    
            request.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            MySingleton.getmInstance(RegisterActivity.this).addToRequestQueue(request);
        }

}

Solution

  • I'd suggest using a Handler perhaps:

    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                // dismiss progress dialog here 
            }
    }, 3000)
    

    This can be done inside the onRepsonse method in your Volley request