1

SITUATION

I have been reading a lot of stuff about this on google but I still can't solve my problem. I have an app that, once you press a button, has to execute a lot of tasks before showing the output. I want to display a progress dialog like this:

enter image description here

Very easy, the circle that is moving on the left and the text "Calculating results..." on the left.


PROBLEM

I have the following code:

public class fragmentMetodoTangenti extends Fragment {

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) { 

  // Inflate the layout for this fragment
  final View view = inflater.inflate(R.layout.fragment_metodo_tangenti, container, false);

  Button button = (Button) view.findViewById(R.id.button1);
  button.setOnClickListener(new View.OnClickListener() {

   @Override
        public void onClick(View v) {


         try {

             loading = new ProgressDialog(view.getContext());
             loading.setCancelable(true);
             loading.setMessage("Calculating results...");
             loading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
             loading.show();

          //other "heavy" code here (a lot of for and while loops)

             loading.dismiss();

         } catch (Exception e) {
          //catching the error
         }


  });

  //return the view
  return view;

 }

}

The code works perfectly because I see the correct output at the end (some numbers and letters). The only problem is that when I click the button I cannot see the Progress Dialog with the spinner inside.

I think that the problem could be on new ProgressDialog(view.getContext()); but I am not sure. I am using the latest version of Android Studio.

How could I solve this?

LOG HERE

Alberto Miola
  • 4,643
  • 8
  • 35
  • 49

2 Answers2

3

You should move your heavy code in an AsynkTask and create the progressDialog in the onPreExecute() callback and dismiss it in the onPostExecute() callback.

More info here: https://developer.android.com/reference/android/os/AsyncTask.html

Lino
  • 5,084
  • 3
  • 21
  • 39
0

The OnClick callback is called on the UI thread, in order to keep the UI smooth you should not do "Heavy" tasks on this thread.

This may be overkill for this case, but you should take a look to https://github.com/ReactiveX/RxAndroid. Trust me, there are a lot of benefit of using RxJava/RxAndroid to work, in this case you could solve the problem by doing:

public void OnClick(View v) {
    Observable.defer(() -> heavyTask())
              .observeOn(Schedulers.computation())
              .subscribeOn(AndroidSchedulers.mainThread())
              .doOnSubscribe(() -> showProgressBar())
              .doOnCompleted(() -> hideProgressBar())
              .subscribe();
}

It's hard at the start, but you end up with really neat code.

Alternatively you can take a look at AsyncTask class which is the standard solution for these kind of problems. You can find a sample here Download a file with Android, and showing the progress in a ProgressDialog

Community
  • 1
  • 1
Pato94
  • 784
  • 5
  • 6