ProgressDialog is deprecated.What is the alternate one to use?

Sunil P picture Sunil P · Jul 28, 2017 · Viewed 193k times · Source

I have come across to see that ProgressDialog is now deprecated. What would be alternate one to use in place of that apart from ProgressBar. I am using android studio version 2.3.3.

ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.show();

Answer

Gowthaman M picture Gowthaman M · Aug 10, 2017

Yes, in API level 26 it's deprecated. Instead, you can use progressBar.

To create it programmatically:

First get a reference to the root layout

RelativeLayout layout = findViewById(R.id.display);  //specify here Root layout Id

or

RelativeLayout layout = findViewById(this);

Then add the progress bar

progressBar = new ProgressBar(youractivity.this, null, android.R.attr.progressBarStyleLarge);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar, params);

To show the progress bar

progressBar.setVisibility(View.VISIBLE);

To hide the progress bar

progressBar.setVisibility(View.GONE);

To disable the user interaction you just need to add the following code

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                           WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

To get user interaction back you just need to add the following code

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

Just for future reference, change the android.R.attr.progressBarStyleSmall to android.R.attr.progressBarStyleHorizontal.

The code below only works above API level 21

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));

To create it via xml:

<ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:max="100"
        android:backgroundTint="@color/white"
        android:layout_below="@+id/framelauout"
        android:indeterminateTint="#1a09d6"
        android:layout_marginTop="-7dp"/>

In your activity

progressBar = (ProgressBar) findViewById(R.id.progressbar);

Showing/hiding the progress bar is the same

 progressBar.setVisibility(View.VISIBLE); // To show the ProgressBar 
 progressBar.setVisibility(View.INVISIBLE); // To hide the ProgressBar

Here is a sample image of what it would look like:

For more details:
1. Reference one
2. Reference Two