Show ProgressBar while task is running
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

add a comment |
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

add a comment |
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

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

java

asked Nov 19 '18 at 16:24


NicolasP
11
11
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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);
}
}
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
add a comment |
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.
add a comment |
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! :)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
}
}
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
add a comment |
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);
}
}
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
add a comment |
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);
}
}
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);
}
}
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 '18 at 16:31
Notsileous
22818
22818
add a comment |
add a comment |
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! :)
add a comment |
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! :)
add a comment |
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! :)
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! :)
answered Nov 19 '18 at 17:06
davy307
80110
80110
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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