I have come across to see that ProgressDialog
is now deprecated. What would be alternate one to use in place of that apart from ProgressBar
.
I am
Well if you really wants to go against their will, still you can use Sweet Alert Dialog or create one on your own.
progress_dialog_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="64dp" >
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:gravity="center|left"
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="18sp"
android:text="Downloading data. Please wait.." />
</TableRow>
</RelativeLayout>
Java code:
AlertDialog b;
AlertDialog.Builder dialogBuilder;
public void ShowProgressDialog() {
dialogBuilder = new AlertDialog.Builder(DataDownloadActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View dialogView = inflater.inflate(R.layout.progress_dialog_layout, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setCancelable(false);
b = dialogBuilder.create();
b.show();
}
public void HideProgressDialog(){
b.dismiss();
}
You can use this class I wrote. It offers only the basic functions. If you want a fully functional ProgressDialog, then use this lightweight library.
Add the following dependency to module/build.gradle:
compile 'com.lmntrx.android.library.livin.missme:missme:0.1.5'
Usage is similar to original ProgressDialog
ProgressDialog progressDialog = new
progressDialog(YourActivity.this);
progressDialog.setMessage("Please wait");
progressDialog.setCancelable(false);
progressDialog.show();
progressDialog.dismiss();
NB: You must override activity's onBackPressed()
Java8 Implementation:
@Override
public void onBackPressed() {
progressDialog.onBackPressed(
() -> {
super.onBackPressed();
return null;
}
);
}
Kotlin Implementation:
override fun onBackPressed() {
progressDialog.onBackPressed { super.onBackPressed() }
}
ProgressBar is best alternative for ProgressDialog. A user interface element that indicates the progress of an operation.
For more info see this Google doc: https://developer.android.com/reference/android/widget/ProgressBar.html
ProgressBar is very simple and easy to use, i am intending to make this same as simple progress dialog. 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
dialog.show(); // to show this dialog
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 instead of ProgressDialog. now you can show/dismiss this dialog box in either Handler or ASyncTask, its up to your need
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. link
It's deprecated at Android O
because of Google
new UI standard
Maybe this guide could help you.
Usually I prefer to make custom AlertDialogs with indicators. It solves such problems like customization of the App view.