Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressDialog is deprecated [duplicate]

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!

like image 515
Alok Avatar asked Jul 27 '17 12:07

Alok


People also ask

Is ProgressDialog deprecated?

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.

How do I instantiate an object for ProgressDialog class?

In order to do this, you need to instantiate an object of this class. Its syntax is. ProgressDialog progress = new ProgressDialog(this); Now you can set some properties of this dialog.

How do you show progress dialog in Kotlin?

Step by Step Implementation We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project. Navigate to the app > res > layout > activity_main. xml and add the below code to that file. Below is the code for the activity_main.


2 Answers

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.

like image 181
Divy Soni Avatar answered Oct 07 '22 11:10

Divy Soni


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

like image 39
Syed Naeem Avatar answered Oct 07 '22 13:10

Syed Naeem