ProgressDialog is deprecated [duplicate]

2019-01-25 09:18发布

This question already has an answer here:

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!

9条回答
别忘想泡老子
2楼-- · 2019-01-25 09:22

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.

查看更多
Animai°情兽
3楼-- · 2019-01-25 09:23

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

查看更多
放荡不羁爱自由
4楼-- · 2019-01-25 09:25

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.

查看更多
劫难
5楼-- · 2019-01-25 09:26

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");
查看更多
Luminary・发光体
6楼-- · 2019-01-25 09:27

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.

查看更多
甜甜的少女心
7楼-- · 2019-01-25 09:33

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

查看更多
登录 后发表回答