Best way to show a loading/progress indicator?

AndroidAndroid Spinner

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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...");

Read more here.

By the way, Spinner has a different meaning in Android. (It's like the select dropdown in HTML)

Solution 2 - Android

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

Solution 3 - Android

Use ProgressDialog

ProgressDialog.show(Context context, CharSequence title, CharSequence message);

enter image description here

However this is considered as an anti pattern today (2013): http://www.youtube.com/watch?v=pEGWcMTxs3I

Solution 4 - Android

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

Solution 5 - Android

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();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJochem GruterView Question on Stackoverflow
Solution 1 - AndroidSuraj BajajView Answer on Stackoverflow
Solution 2 - AndroidIlker BaltaciView Answer on Stackoverflow
Solution 3 - AndroidvasartView Answer on Stackoverflow
Solution 4 - AndroidquinestorView Answer on Stackoverflow
Solution 5 - AndroidIsaac ZaisView Answer on Stackoverflow