ProgressDialog is deprecated [duplicate]
Asked Answered
S

9

18

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!

Spandex answered 27/7, 2017 at 12:41 Comment(5)
When you use progresBar the progressBar not shows?Rockies
nopes! I have done the same thing just used the progressBar instead. I did setVisibility(View.VISIBILE) and setVisibility(View.INVISIBLE) for the same but no sign.Spandex
Can you post the sign in code?Rockies
I don't have any sign in code. It is just that I'm moving from this activity to the fragment. Which is taking a little bit of time. So I was thinking to implement the progressDialogSpandex
@Spandex 's problem was calling the dismiss() method right after starting sending the intent. The dialog "will" appear and desapear very fast, which, by the users perspective, it will seems it never appeared. Answers like "it was deprecated" or "implement a custom dialog" don't help, especially the ones about creating a custom dialog, because it not only don't talk about the real problem, but it also doesn't help with the usability issue, which is the reason why this class was deprecated in the first place.Briney
M
18

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.

Madgemadhouse answered 27/7, 2017 at 12:58 Comment(4)
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?Spandex
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!Spandex
Awesome Bro :) That worked for me thank you so much for this :)Intertype
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 UIDispleasure
L
15

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

Libbi answered 5/8, 2018 at 12:47 Comment(0)
J
5

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.

Jocasta answered 19/8, 2017 at 4:49 Comment(1)
more reference #45373507Jocasta
I
2

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

Import answered 1/8, 2017 at 11:48 Comment(0)
P
2

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");
Passepartout answered 17/9, 2017 at 8:26 Comment(0)
N
2

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.

Navicert answered 2/3, 2018 at 12:16 Comment(0)
G
1

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

Gabble answered 8/9, 2017 at 11:58 Comment(0)
O
0

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.

Onslaught answered 14/12, 2017 at 14:47 Comment(0)
L
-3

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.

Lithology answered 18/9, 2017 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.