ProgressBar won't show when I call setVisibility(View.Visible) - android

In an Android app, I am trying to make a ProgressBar visible when a button is clicked, conduct some processes, and then make the bar invisible again. A general overview of my button's onClick method is shown below:
ProgressBar bar = (ProgressBar)findViewById(R.id.prog_bar);
bar.setVisibility(View.VISIBLE);
...do some stuff...
bar.setVisibility(View.INVISIBLE);
However, when I run the code, while the process in the middle is completed, the ProgressBar never shows up. Any ideas?

OnClick is called on the main thread. Your changes to the ProgressBar will not have any visible effect until your OnClick method returns. By that time, you've already finished all your work and hidden it again.
Move your work to a background thread. Not only will this make your app more responsive, it will allow the prograss bar to appear. (Note that, when your work is finished, you'll have to hide the progress bar from the main thread, not the worker thread. Typically you post a Message to a Handler to achieve this.)

Related

Make user interaction inactive while progress bar is visible

In a dialog fragment , when i use a progress bar (not progress dialog) I want to complete a task and then allow user interaction. It can be achieved with progress dialog by using setCancelable(false) , but how to do it with progress bar ?
(Since, getWindow() function doesn't work inside a dialog fragment).
Thank you in advance :)
I would not recommend you doing that since the idea of progress bar is to show the user that something is going on but it should not have anything to do with the fact that they can interact with the screen. (That's one of the reasons why ProgressDialog was deprecated, to encourage not to block the UI when something is loading).
Besides that, answering your question, getWindow can be called from the activity, you can retrieve the activity in your dialog fragment using either requireActivity().getWindow() or getActicity().getWindow() and set the params accordingly to disable user action.
You can simply add isLoading boolean flag, and forbid all interactions while that flag is true, and after the work is done, set that flag to false

Updating Custom listview item after on resume()

I have a custum listview with image, text, a download button and a progress bar for each item.
The plan is that when I click the download button, the progress bar which original is invisible becomes visible and starts to progress, while the download button changes to a cancel button.
But what happens now is that when I minimize the app to home and opens it back up, the listview refreshes(that is the adapter is called again) as the activity resumes. This undos all the states which have been set.
the progress bar becomes invisible and the download button shows up with the download icon instead of the cancel icon, even though the download continues in Asynctask.
What I need to do is to find a to set the state of the list item, such that the progress bar continues to progress and the button changes to cancel button. i.e to retain the ui state as the download goes on in the Asynctask, even when the listview is reinitialized.
I viewed` your question. The problem is regarding the Asynchronous AsyncTask that you used. I can try to explain you the problem and the recommended solution.
If you start the AsyncTask inside your activity, Now when you close the activity (finish) It is not guaranteed that the running AsyncTask also stop working and finish itself because of your activity is no longer running. That does not happen.
Another fact is that, As previously said that once you fire the AsyncTask you could not reuse it next time when you start the same activity again. In your case, it always starts the new AsyncTask when you open the activity.
Alternative Solution: You can use the Service component that can run in the background (keep in mind that it does not create the separate thread from your application, It's run on the application thread) means it has no UI and could be run even if your activity or application is not in front of the user. You can put your download logic inside it and bind with your running activity.
Regarding the state of the UI in adapter that you can manage by communicating between the activity and running service.
This is simple approach that you can use it.
Hope this will help!
Thanks
Bhavdip

Android indeterminate progress bar between activities

How to get an indeterminate progress bar between two activities, I mean, I'm in activity A, show the progress bar, while the progress bar is showing, a new activity B is started and in this activity B I want to close the progress bar. Thank you.
You can't do it that way. An Activity is what holds everything on the screen for your application. If you are "inbetween" two activities then you have no way to control what is on the screen.
You need to put the progress bar into ActivityB, and make it visible by default, then when you are done loading your data make it invisible with
progressBar.setVisibility(View.GONE);
If your ActivityB is taking a long time to load before showing any Views then it is an indication that you are doing some loading work on the Main thread, that you should isntead be doing in the background, and updating the Views once loading is complete.
There is no reason to do so. Starting a new activity is a very fast process, almost instant (unless you onPause method takes very long), so the progress bar will never be shown.
That being said, there is a hacky way to get a progress bar to be shown between activities
you call ProgressActivity(that you create) from ActivityA then after waiting for some time (delayed runnable) you call ActivityB. This does nothing constructive and should not be used.

Custom ProgressBar not spinning, when open from onResume()

I have a custom progress bar in my app. When I open my app, this Custom ProgressBar works well, but in some situations where my app calls itself, the onCreate() event does not called.
The app starts from an onResume() event. I have created a function to open the progress bar that is called in the onResume() event. The problem is that the custom Progress Bar gets opened but does not start SPINNING, and it is dismissed after the task is completed. It therefor appears as if the application is hanging.
Are there any solution for this?

Android - Indeterminate ProgressBar

I am using the code from this website (http://www.helloandroid.com/tutorials/using-threads-and-progressdialog) in my App.
It is somewhat working, but not entirely:
When I press Button X, a method is called, and while it is working, the progress bar is shown. After the method is done, another activity is started. So far everything is working fine. But when I press the "BACK" button on my phone, the progress bar is shown again, and appears to be doing nothing.
What can I do to prevent that?
Dismiss the progress bar at the same time that you start the second activity.

Categories

Resources