ProgressDialog is deprecated

51,840

Solution 1

As it is mentioned in Android O documentation:

This class was deprecated in API level 26. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

You can create a custom view with TextView and ProgressBar and manage its visibilty. You can use this library also because it is using AlertDialog instead of ProgressDialog.

Solution 2

ProgressBar is very simple and easy to use, first step is that you can make xml layout of the dialog that you want to show, let say we name this layout

layout_loading_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp">
    <ProgressBar
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:gravity="center"
        android:text="Please wait! This may take a moment." />
</LinearLayout>

next step is create AlertDialog which will show this layout with ProgressBar

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false); // if you want user to wait for some process to finish,
builder.setView(R.layout.layout_loading_dialog);
AlertDialog dialog = builder.create();

now all that is left is to show and hide this dialog in our click events like this

progress_dialog.show(); // to show this dialog
progress_dialog.dismiss(); // to hide this dialog

and thats it, it should work, as you can see it is farely simple and easy to implement ProgressBar (like ProgressDialog) instead of deprecated ProgressDialog. now you can show/dismiss this dialog box in either Handler or ASyncTask, its up to your need, hope you can use this to solve your problems, cheers

Solution 3

Yes, API level 26 it's deprecated, Better you can use progressbar only.

Use this code snippet for creating programmatically:

ProgressBar progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);

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

Maybe this guide could help you.

I hope this may help you.

Solution 4

You can use ProgressBar instead of ProgressDialog. Create a ProgressBar inside a custom dialog with TextView and other widgets you need.

Solution 5

You need to create a custom XML layout file with ProgressBar on it and show that instead. I've been using a library like https://github.com/Q115/DelayedProgressDialog to get this simple behavior.

Usage:

DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
Share:
51,840
Alok
Author by

Alok

A hands-on creative software developer with excellent communication, organizational skills. A hard-working person who believes in completing things, who has an experience in Object-Oriented Programming as well as working on different platforms. Great dedication with excellent focus on doing various task. A learning enthusiast who is comfortable with multiple tech languages &amp; frameworks and would love to work in a very dynamic &amp; aspirational environment.

Updated on July 15, 2022

Comments

  • Alok
    Alok almost 2 years

    Since the ProgressDialog is deprecated from the Android version O, I'm still finding a better way out to do my task. The task is to move from my activity to the fragment. Everything is working fine but the progressdialog is not visible. I've tried implementing it but... the progressdialog doesn't work.

    It seems the progressbar would work but still not working. I need a progressdialog because it is simply easy for me to set my title and the message. I need a spinner progressDialog but don't know how to do it. Here is one of my work but not implementing :

    Java Class

    ublic class SaveVideo extends AppCompatActivity {
    
    private Button button;
    
    private ProgressDialog mProgressDialog;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_save_video);
    
        mProgressDialog = new ProgressDialog(this);
    
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
        button = (Button) findViewById(R.id.saveVideo);
    
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                //where it must be seen when the button is pressed
                mProgressDialog.setTitle("Title");
                mProgressDialog.setMessage("Message");
                mProgressDialog.show();
    
                Intent intent = new Intent(SaveVideo.this,MainActivity.class);
                intent.putExtra("change",2);
                startActivity(intent);
    
                //as soon as the page moves from this to another fragment
                mProgressDialog.dismiss();
            }
        });
    
    
    }
    

    I'm new to Android Version O. Any help would give me new thing to learn!

  • Alok
    Alok almost 7 years
    do you believe that'd work if I want a spinner progressbar just like the progressDialog. The title and other messages? does the progress bar shows up when we are moving from ativity to fragment?
  • Alok
    Alok almost 7 years
    I've used this library but the thing it said is not working like AlertDialog alertdialog = new SpotsDialog(context); this is giving me an error and I've changed it to the SpotsDailog from AlertDialog and still it doesn't show up. As I'm just normally moving from one activity to another without any progressbar!
  • Swifty Codes
    Swifty Codes almost 7 years
    Awesome Bro :) That worked for me thank you so much for this :)
  • Gowthaman M
    Gowthaman M over 6 years
  • vuhung3990
    vuhung3990 almost 6 years
    You can use this library also because it is using AlertDialog instead of ProgressDialog use another kind of dialog is bad idea, document say you should use a progress indicator like ProgressBar, which can be embedded in your app's UI