Search code examples
androidprogressdialog

Alternate of ProgressDialog


In API level 26, ProgressDialog is deprecated. In my app I want to show the progress of my downloading task with title and cancel option. What would be the alternate option to do this.


Solution

  • Why not adding a ProgressView to your Dialog view without creating a custom layout for Dialog

      LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setGravity(Gravity.CENTER);
            ProgressBar progressBar = new ProgressBar(this);
            LinearLayout.LayoutParams progressParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            progressParam.setMargins(0, 40, 0, 40);// set margin to progressBar
            progressBar.setLayoutParams(progressParam);
            linearLayout.addView(progressBar);
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Loading ")
                    .setMessage("Please waite until the map loaded")
                    .setView(linearLayout).setCancelable(false)
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();     
                        }
                    });
                    
            builder.create().show();