Show ProgressBar while task is running












0














I'm trying to show a progress bar only while a task is running, this is my code



public void onButtonPressed() {
//loadingSpinner is a ProgressBar already instantiated whit visibility GONE;
loadingSpinner.setVisibility(View.VISIBLE);
boolean resultFromAsyncTask = AnotherClass.AsyncTaskMethod();
if(resultFromAsyncTask ) {
loadingSpinner.setVisibility(View.GONE);
//do something
finish();
}else{
loadingSpinner.setVisibility(View.GONE);
//Show alert
}
}


The problem is that when I change visibility the first time nothing happens. What am I missing?










share|improve this question



























    0














    I'm trying to show a progress bar only while a task is running, this is my code



    public void onButtonPressed() {
    //loadingSpinner is a ProgressBar already instantiated whit visibility GONE;
    loadingSpinner.setVisibility(View.VISIBLE);
    boolean resultFromAsyncTask = AnotherClass.AsyncTaskMethod();
    if(resultFromAsyncTask ) {
    loadingSpinner.setVisibility(View.GONE);
    //do something
    finish();
    }else{
    loadingSpinner.setVisibility(View.GONE);
    //Show alert
    }
    }


    The problem is that when I change visibility the first time nothing happens. What am I missing?










    share|improve this question

























      0












      0








      0







      I'm trying to show a progress bar only while a task is running, this is my code



      public void onButtonPressed() {
      //loadingSpinner is a ProgressBar already instantiated whit visibility GONE;
      loadingSpinner.setVisibility(View.VISIBLE);
      boolean resultFromAsyncTask = AnotherClass.AsyncTaskMethod();
      if(resultFromAsyncTask ) {
      loadingSpinner.setVisibility(View.GONE);
      //do something
      finish();
      }else{
      loadingSpinner.setVisibility(View.GONE);
      //Show alert
      }
      }


      The problem is that when I change visibility the first time nothing happens. What am I missing?










      share|improve this question













      I'm trying to show a progress bar only while a task is running, this is my code



      public void onButtonPressed() {
      //loadingSpinner is a ProgressBar already instantiated whit visibility GONE;
      loadingSpinner.setVisibility(View.VISIBLE);
      boolean resultFromAsyncTask = AnotherClass.AsyncTaskMethod();
      if(resultFromAsyncTask ) {
      loadingSpinner.setVisibility(View.GONE);
      //do something
      finish();
      }else{
      loadingSpinner.setVisibility(View.GONE);
      //Show alert
      }
      }


      The problem is that when I change visibility the first time nothing happens. What am I missing?







      java android android-asynctask






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 16:24









      NicolasP

      11




      11
























          3 Answers
          3






          active

          oldest

          votes


















          2














          Don't over complicate things, AsyncTask has all the methods you need to accomplish this.



          Set loadingSpinner.setVisibility(View.VISIBLE); inside onPreExecute of your AsyncTask and set loadingSpinner.setVisibility(View.GONE); inside onPostExecute.



          Like this:



          private class YourTask extends AsyncTask<String, Void, String> { 

          @Override protected void onPreExecute(){
          loadingSpinner.setVisibility(View.VISIBLE);
          }


          @Override
          protected String doInBackground(String... params) {
          //Do background work
          }

          @Override protected void onPostExecute(String result) {
          loadingSpinner.setVisibility(View.GONE);
          }

          }





          share|improve this answer























          • This seems to be the simpliest way and I tried but doesn't work. The spinner is visible only in onPostExecute phase (I've added a Thread.sleep just before setVisibility(View.GONE) to see it)
            – NicolasP
            Nov 20 '18 at 10:05










          • @NicolasP please update your question and provide your code how you implemented it.
            – HB.
            Nov 20 '18 at 10:15



















          1














          Your calling an Async task but using it like a regular function. You cant base something off the result of an Async task like that, the code will just run right over it and execute the next lines.

          What you want is to show the progress bar, then start your task without a return like that. You need something like a broadcast from your onPostExecute to call back to your calling class to let it know when its done.

          Edit** in this case since a bool defaults to false, it is using that value in your if statement and hiding it as soon as you show it.






          share|improve this answer





























            0














            Building on @Notsileous, call a method from the onPostExecute method of your AsyncTask that does some UI work. You may need to wrap that code in a runOnUiThread for it to work.



            To exemplify with your code:



            public void onButtonPressed() {
            //loadingSpinner is a ProgressBar already instantiated whit visibility GONE;
            loadingSpinner.setVisibility(View.VISIBLE);
            AnotherClass.AsyncTaskMethod().execute();
            }
            [...]
            public void doneLoading(boolean resultFromAsyncTask ) {
            runOnUiThread {
            if(resultFromAsyncTask ) {
            loadingSpinner.setVisibility(View.GONE);
            //do something
            finish();
            }else{
            loadingSpinner.setVisibility(View.GONE);
            //Show alert
            }
            }
            }
            [...]
            AsyncTask postExecuteMethod(boolean executionResult) {
            doneLoading(executionResult);
            }
            [...]


            Hope this helps clarify! :)






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378838%2fshow-progressbar-while-task-is-running%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Don't over complicate things, AsyncTask has all the methods you need to accomplish this.



              Set loadingSpinner.setVisibility(View.VISIBLE); inside onPreExecute of your AsyncTask and set loadingSpinner.setVisibility(View.GONE); inside onPostExecute.



              Like this:



              private class YourTask extends AsyncTask<String, Void, String> { 

              @Override protected void onPreExecute(){
              loadingSpinner.setVisibility(View.VISIBLE);
              }


              @Override
              protected String doInBackground(String... params) {
              //Do background work
              }

              @Override protected void onPostExecute(String result) {
              loadingSpinner.setVisibility(View.GONE);
              }

              }





              share|improve this answer























              • This seems to be the simpliest way and I tried but doesn't work. The spinner is visible only in onPostExecute phase (I've added a Thread.sleep just before setVisibility(View.GONE) to see it)
                – NicolasP
                Nov 20 '18 at 10:05










              • @NicolasP please update your question and provide your code how you implemented it.
                – HB.
                Nov 20 '18 at 10:15
















              2














              Don't over complicate things, AsyncTask has all the methods you need to accomplish this.



              Set loadingSpinner.setVisibility(View.VISIBLE); inside onPreExecute of your AsyncTask and set loadingSpinner.setVisibility(View.GONE); inside onPostExecute.



              Like this:



              private class YourTask extends AsyncTask<String, Void, String> { 

              @Override protected void onPreExecute(){
              loadingSpinner.setVisibility(View.VISIBLE);
              }


              @Override
              protected String doInBackground(String... params) {
              //Do background work
              }

              @Override protected void onPostExecute(String result) {
              loadingSpinner.setVisibility(View.GONE);
              }

              }





              share|improve this answer























              • This seems to be the simpliest way and I tried but doesn't work. The spinner is visible only in onPostExecute phase (I've added a Thread.sleep just before setVisibility(View.GONE) to see it)
                – NicolasP
                Nov 20 '18 at 10:05










              • @NicolasP please update your question and provide your code how you implemented it.
                – HB.
                Nov 20 '18 at 10:15














              2












              2








              2






              Don't over complicate things, AsyncTask has all the methods you need to accomplish this.



              Set loadingSpinner.setVisibility(View.VISIBLE); inside onPreExecute of your AsyncTask and set loadingSpinner.setVisibility(View.GONE); inside onPostExecute.



              Like this:



              private class YourTask extends AsyncTask<String, Void, String> { 

              @Override protected void onPreExecute(){
              loadingSpinner.setVisibility(View.VISIBLE);
              }


              @Override
              protected String doInBackground(String... params) {
              //Do background work
              }

              @Override protected void onPostExecute(String result) {
              loadingSpinner.setVisibility(View.GONE);
              }

              }





              share|improve this answer














              Don't over complicate things, AsyncTask has all the methods you need to accomplish this.



              Set loadingSpinner.setVisibility(View.VISIBLE); inside onPreExecute of your AsyncTask and set loadingSpinner.setVisibility(View.GONE); inside onPostExecute.



              Like this:



              private class YourTask extends AsyncTask<String, Void, String> { 

              @Override protected void onPreExecute(){
              loadingSpinner.setVisibility(View.VISIBLE);
              }


              @Override
              protected String doInBackground(String... params) {
              //Do background work
              }

              @Override protected void onPostExecute(String result) {
              loadingSpinner.setVisibility(View.GONE);
              }

              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 19 '18 at 17:10

























              answered Nov 19 '18 at 17:05









              HB.

              1,5252923




              1,5252923












              • This seems to be the simpliest way and I tried but doesn't work. The spinner is visible only in onPostExecute phase (I've added a Thread.sleep just before setVisibility(View.GONE) to see it)
                – NicolasP
                Nov 20 '18 at 10:05










              • @NicolasP please update your question and provide your code how you implemented it.
                – HB.
                Nov 20 '18 at 10:15


















              • This seems to be the simpliest way and I tried but doesn't work. The spinner is visible only in onPostExecute phase (I've added a Thread.sleep just before setVisibility(View.GONE) to see it)
                – NicolasP
                Nov 20 '18 at 10:05










              • @NicolasP please update your question and provide your code how you implemented it.
                – HB.
                Nov 20 '18 at 10:15
















              This seems to be the simpliest way and I tried but doesn't work. The spinner is visible only in onPostExecute phase (I've added a Thread.sleep just before setVisibility(View.GONE) to see it)
              – NicolasP
              Nov 20 '18 at 10:05




              This seems to be the simpliest way and I tried but doesn't work. The spinner is visible only in onPostExecute phase (I've added a Thread.sleep just before setVisibility(View.GONE) to see it)
              – NicolasP
              Nov 20 '18 at 10:05












              @NicolasP please update your question and provide your code how you implemented it.
              – HB.
              Nov 20 '18 at 10:15




              @NicolasP please update your question and provide your code how you implemented it.
              – HB.
              Nov 20 '18 at 10:15













              1














              Your calling an Async task but using it like a regular function. You cant base something off the result of an Async task like that, the code will just run right over it and execute the next lines.

              What you want is to show the progress bar, then start your task without a return like that. You need something like a broadcast from your onPostExecute to call back to your calling class to let it know when its done.

              Edit** in this case since a bool defaults to false, it is using that value in your if statement and hiding it as soon as you show it.






              share|improve this answer


























                1














                Your calling an Async task but using it like a regular function. You cant base something off the result of an Async task like that, the code will just run right over it and execute the next lines.

                What you want is to show the progress bar, then start your task without a return like that. You need something like a broadcast from your onPostExecute to call back to your calling class to let it know when its done.

                Edit** in this case since a bool defaults to false, it is using that value in your if statement and hiding it as soon as you show it.






                share|improve this answer
























                  1












                  1








                  1






                  Your calling an Async task but using it like a regular function. You cant base something off the result of an Async task like that, the code will just run right over it and execute the next lines.

                  What you want is to show the progress bar, then start your task without a return like that. You need something like a broadcast from your onPostExecute to call back to your calling class to let it know when its done.

                  Edit** in this case since a bool defaults to false, it is using that value in your if statement and hiding it as soon as you show it.






                  share|improve this answer












                  Your calling an Async task but using it like a regular function. You cant base something off the result of an Async task like that, the code will just run right over it and execute the next lines.

                  What you want is to show the progress bar, then start your task without a return like that. You need something like a broadcast from your onPostExecute to call back to your calling class to let it know when its done.

                  Edit** in this case since a bool defaults to false, it is using that value in your if statement and hiding it as soon as you show it.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 16:31









                  Notsileous

                  22818




                  22818























                      0














                      Building on @Notsileous, call a method from the onPostExecute method of your AsyncTask that does some UI work. You may need to wrap that code in a runOnUiThread for it to work.



                      To exemplify with your code:



                      public void onButtonPressed() {
                      //loadingSpinner is a ProgressBar already instantiated whit visibility GONE;
                      loadingSpinner.setVisibility(View.VISIBLE);
                      AnotherClass.AsyncTaskMethod().execute();
                      }
                      [...]
                      public void doneLoading(boolean resultFromAsyncTask ) {
                      runOnUiThread {
                      if(resultFromAsyncTask ) {
                      loadingSpinner.setVisibility(View.GONE);
                      //do something
                      finish();
                      }else{
                      loadingSpinner.setVisibility(View.GONE);
                      //Show alert
                      }
                      }
                      }
                      [...]
                      AsyncTask postExecuteMethod(boolean executionResult) {
                      doneLoading(executionResult);
                      }
                      [...]


                      Hope this helps clarify! :)






                      share|improve this answer


























                        0














                        Building on @Notsileous, call a method from the onPostExecute method of your AsyncTask that does some UI work. You may need to wrap that code in a runOnUiThread for it to work.



                        To exemplify with your code:



                        public void onButtonPressed() {
                        //loadingSpinner is a ProgressBar already instantiated whit visibility GONE;
                        loadingSpinner.setVisibility(View.VISIBLE);
                        AnotherClass.AsyncTaskMethod().execute();
                        }
                        [...]
                        public void doneLoading(boolean resultFromAsyncTask ) {
                        runOnUiThread {
                        if(resultFromAsyncTask ) {
                        loadingSpinner.setVisibility(View.GONE);
                        //do something
                        finish();
                        }else{
                        loadingSpinner.setVisibility(View.GONE);
                        //Show alert
                        }
                        }
                        }
                        [...]
                        AsyncTask postExecuteMethod(boolean executionResult) {
                        doneLoading(executionResult);
                        }
                        [...]


                        Hope this helps clarify! :)






                        share|improve this answer
























                          0












                          0








                          0






                          Building on @Notsileous, call a method from the onPostExecute method of your AsyncTask that does some UI work. You may need to wrap that code in a runOnUiThread for it to work.



                          To exemplify with your code:



                          public void onButtonPressed() {
                          //loadingSpinner is a ProgressBar already instantiated whit visibility GONE;
                          loadingSpinner.setVisibility(View.VISIBLE);
                          AnotherClass.AsyncTaskMethod().execute();
                          }
                          [...]
                          public void doneLoading(boolean resultFromAsyncTask ) {
                          runOnUiThread {
                          if(resultFromAsyncTask ) {
                          loadingSpinner.setVisibility(View.GONE);
                          //do something
                          finish();
                          }else{
                          loadingSpinner.setVisibility(View.GONE);
                          //Show alert
                          }
                          }
                          }
                          [...]
                          AsyncTask postExecuteMethod(boolean executionResult) {
                          doneLoading(executionResult);
                          }
                          [...]


                          Hope this helps clarify! :)






                          share|improve this answer












                          Building on @Notsileous, call a method from the onPostExecute method of your AsyncTask that does some UI work. You may need to wrap that code in a runOnUiThread for it to work.



                          To exemplify with your code:



                          public void onButtonPressed() {
                          //loadingSpinner is a ProgressBar already instantiated whit visibility GONE;
                          loadingSpinner.setVisibility(View.VISIBLE);
                          AnotherClass.AsyncTaskMethod().execute();
                          }
                          [...]
                          public void doneLoading(boolean resultFromAsyncTask ) {
                          runOnUiThread {
                          if(resultFromAsyncTask ) {
                          loadingSpinner.setVisibility(View.GONE);
                          //do something
                          finish();
                          }else{
                          loadingSpinner.setVisibility(View.GONE);
                          //Show alert
                          }
                          }
                          }
                          [...]
                          AsyncTask postExecuteMethod(boolean executionResult) {
                          doneLoading(executionResult);
                          }
                          [...]


                          Hope this helps clarify! :)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 '18 at 17:06









                          davy307

                          80110




                          80110






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378838%2fshow-progressbar-while-task-is-running%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith