ProgressDialog is deprecated [duplicate]

为君一笑 提交于 2019-11-28 10:00:54

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.

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

Gowthaman M

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.

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.

https://developer.android.com/reference/android/app/ProgressDialog.html

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");
Milad Moosavi

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

This is what i managed to put together since the class has been deprecated in Android Oreo (API 26 +).

In the Xml File (whatever layout file):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:padding="13dp"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ProgressBar
            android:visibility="gone"
            android:layout_marginTop="5dp"
            android:id="@+id/top_progressBar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_height="wrap_content"
            android:indeterminateTintMode="src_in"
            android:indeterminateTint="@color/white"
            android:indeterminate="true" />
    <TextView
        android:layout_width="wrap_content"
        android:text="Loading..."
        android:textAppearance="?android:textAppearanceSmall"
        android:layout_gravity="center_vertical"
        android:id="@+id/loading_msg"
        android:layout_toEndOf="@+id/loader"
        android:layout_height="wrap_content" />
  <ProgressBar
                android:visibility="gone"
                android:layout_marginTop="2dp"
                android:id="@+id/down_progressBar"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_height="wrap_content"
                android:indeterminateTintMode="src_in"
                android:indeterminateTint="@color/white"
                android:indeterminate="true" />

</LinearLayout>

in the sample above, i have thought of a scroll situation say your view is long hence the two progress bars.

in the Java file sample :

public class GetUserDetails extends AppCompatActivity {
private ProgressBar topProgressBar, downProgressBar;
    private ProgressDialog progressDialog;
@Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_get_user_details );
//initilise the progressbar views and progress dialog object
 topProgressBar = findViewById ( R.id.top_progressBar );
    downProgressBar = findViewById ( R.id.down_progressBar );
  if ( Build.VERSION.SDK_INT < 26 ) {
        progressDialog = new ProgressDialog ( this );
    } else {
        topProgressBar.setIndeterminate ( true );
        downProgressBar.setIndeterminate ( true );
    }
 }



    private void showProgressDialog (final boolean isToShow) {
            if ( Build.VERSION.SDK_INT < 26 ) {
                if ( isToShow ) {

                    progressDialog.setMessage ( "Processing ...Please wait." );
                    progressDialog.setCancelable ( false );
                    if ( ! progressDialog.isShowing () ) {
                        progressDialog.show ();

                    }

                } else {
                    if ( progressDialog.isShowing () ) {
                        progressDialog.dismiss ();
                    }
                }
            } else {
                /* this is Android Oreo and above*/
                if ( isToShow ) {
                    topProgressBar.setVisibility ( View.VISIBLE );
                    downProgressBar.setVisibility ( View.VISIBLE );
                    getWindow ().setFlags ( WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE );
                } else {
                    topProgressBar.setVisibility ( View.GONE );
                    downProgressBar.setVisibility ( View.GONE );
                    getWindow ().clearFlags ( WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE );
                }
            }
        }

}

Well , this is my hack so i hope it helps.

If anyone insists on having a progress dialog, in my case I opted for a progress bar inside an alert dialog. You can use the following code to get started.

My case was simple because I just needed an indeterminate progressbar. For a full fledged version you'll have to encapsulate it into a class and access the Bar.

private AlertDialog Create_Indeterminate_HorizontalProgressBar_AlertDialog(
        Context context, String title, String message)
{
    final ProgressBar progressBar =
            new ProgressBar(
                    context,
                    null,
                    android.R.attr.progressBarStyleHorizontal);

    progressBar.setLayoutParams(
            new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));

    progressBar.setIndeterminate(true);

    final LinearLayout container =
            new LinearLayout(context);

    container.addView(progressBar);

    int padding =
            getDialogPadding(context);

    container.setPadding(
            padding, (message == null ? padding : 0), padding, 0);

    AlertDialog.Builder builder =
            new AlertDialog.Builder(context).
                    setTitle(title).
                    setMessage(message).
                    setView(container);

    return builder.create();
}

private int getDialogPadding(Context context)
{
    int[] sizeAttr = new int[] { android.support.v7.appcompat.R.attr.dialogPreferredPadding };
    TypedArray a = context.obtainStyledAttributes((new TypedValue()).data, sizeAttr);
    int size = a.getDimensionPixelSize(0, -1);
    a.recycle();

    return size;
}

Note: If you're wondering why the Bar is in a container: I just couldn't get the padding to work on the Bar having to put in on the container instead.

The ProgressDialog in your example won't ever be visible because you call dismiss() right after show(). The creation of an Intent and call to startActivity() are not blocking: Basically you just schedule a switch to the other activity to be performed "soon".

You have to move the dismiss() call to your activity's onStop:

@Override
protected void onStop()
{
    super.onStop();
    mProgressDialog.dismiss();
}

Furthermore one might ask: Why does switching from one activity to the other take so long in this case? I guess that your MainActivity does some heavy work in its onCreate / onStart / onResume methods. A better way of handling that might be to put all that work into a separate thread.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!