What is the best way to show a loading spinner while the app is waiting for a response from the server?
Can this be done programmatically? So that I don't have to add the load spinner in the xml file?
What is the best way to show a loading spinner while the app is waiting for a response from the server?
Can this be done programmatically? So that I don't have to add the load spinner in the xml file?
ProgressDialog is deprecated from Android Oreo. Use ProgressBar instead
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
// To dismiss the dialog
progress.dismiss();
OR
ProgressDialog.show(this, "Loading", "Wait while loading...");
By the way, Spinner has a different meaning in Android. (It's like the select dropdown in HTML)
progress.setCancelable(false);
. –
Bilyeu ProgressDialog
was deprecated in Android O developer.android.com/reference/android/app/ProgressDialog.html –
Seldon ProgressDialog has become deprecated since API Level 26 https://developer.android.com/reference/android/app/ProgressDialog.html
I include a ProgressBar in my layout
<ProgressBar
android:layout_weight="1"
android:id="@+id/progressBar_cyclic"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:minWidth="40dp" />
and change its visibility to .GONE | .VISIBLE depending on the use case.
progressBar_cyclic.visibility = View.VISIBLE
View.GONE
, it doesn't render in UI at all and has no negative effect to performance. View.INVISIBLE
is like making it transparent, but it's in UI thus worse for performance. –
Vizzone Use ProgressDialog
ProgressDialog.show(Context context, CharSequence title, CharSequence message);
However this is considered as an anti pattern today (2013): http://www.youtube.com/watch?v=pEGWcMTxs3I
ProgressDialog
was deprecated in Android O developer.android.com/reference/android/app/ProgressDialog.html –
Seldon Actually if you are waiting for response from a server it should be done programatically. You may create a progress dialog and dismiss it, but then again that is not "the android way".
Currently the recommended method is to use a DialogFragment :
public class MySpinnerDialog extends DialogFragment {
public MySpinnerDialog() {
// use empty constructors. If something is needed use onCreate's
}
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
_dialog = new ProgressDialog(getActivity());
this.setStyle(STYLE_NO_TITLE, getTheme()); // You can use styles or inflate a view
_dialog.setMessage("Spinning.."); // set your messages if not inflated from XML
_dialog.setCancelable(false);
return _dialog;
}
}
Then in your activity you set your Fragment manager and show the dialog once the wait for the server started:
FragmentManager fm = getSupportFragmentManager();
MySpinnerDialog myInstance = new MySpinnerDialog();
}
myInstance.show(fm, "some_tag");
Once your server has responded complete you will dismiss it:
myInstance.dismiss()
Remember that the progressdialog is a spinner or a progressbar depending on the attributes, read more on the api guide
ProgressDialog
was deprecated in Android O developer.android.com/reference/android/app/ProgressDialog.html –
Seldon This is how I did this so that only one progress dialog can be open at a time. Based off of the answer from Suraj Bajaj
private ProgressDialog progress;
public void showLoadingDialog() {
if (progress == null) {
progress = new ProgressDialog(this);
progress.setTitle(getString(R.string.loading_title));
progress.setMessage(getString(R.string.loading_message));
}
progress.show();
}
public void dismissLoadingDialog() {
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
}
I also had to use
protected void onResume() {
dismissLoadingDialog();
super.onResume();
}
ProgressDialog
was deprecated in Android O developer.android.com/reference/android/app/ProgressDialog.html –
Seldon Here comes the one and only version of "how to create a progress dialog because our loved ProgressDialog is deprecated" in Java:
Note: Used with Android 33.
private AlertDialog progressAlertDialog = createProgressDialog(currentActivity / this);
private AlertDialog createProgressDialog(AppCompatActivity currentActivity) {
LinearLayout vLayout = new LinearLayout(currentActivity);
vLayout.setOrientation(LinearLayout.VERTICAL);
vLayout.setPadding(50, 50, 50, 50);
vLayout.addView(new ProgressBar(currentActivity, null, android.R.attr.progressBarStyleLarge));
return new AlertDialog.Builder(currentActivity)
.setCancelable(false)
.setView(vLayout)
.create();
}
public void displayProgressDialog() {
if (!progressAlertDialog.isShowing()) {
progressAlertDialog.show();
}
}
public void hideProgressDialog() {
progressAlertDialog.dismiss();
}
© 2022 - 2024 — McMap. All rights reserved.