Easiest way to implement visual-feedback/busy-indicator in android - android

I am creating an app MainActivity that can calculate a report from its database to be shown in ReportActivity. All report calculation is done in ReportActivity.onCreate().
When the user click on MainActivity-s menu "generate report" the menĂ¼ remains open for 2 secs until ReportActivity.onCreate() has finished and the report becomes visible.
What is the easiest way to give the user some visual feedback?
I already found ProgressDialog onCreate that shows a progressdialog while doing all calculation in an AsyncTask.
I wonder if there is some easier way to give the user some visual feedbak that the menue was successfully clicked and that the device has not crashed.
in ms windows i would use a waitcursor (hour-glass)
I already tried to open a ProgressDialog() in oncreate of the ReportActivity
final ProgressDialog progressDialog = ProgressDialog.show(this, "MY Dialog", "Please wait...");
but it is not shown at all.
Update final solution:
As the answerss suggested I implemented an AsyncTask for the processing following this tutorial
based on this codesample

You need to move your long-running code into a background thread. Long-running code on the UI thread will lock up the UI, which causes the user to think the app is frozen/crashing. Look into using AsyncTask. This allows you to run a process on a background thread and also give visual feedback to let the user know that your app is working.

Design a layout with ProgressDialog. Use setContentView(Progressdialog). Count time for 2-4 seconds (you choice) again set your older layout in setContentView. Why don't you use AsyncTask?

Use a bar or circle activity indicator. From the official design guide:
Activity indicators are for operations of an indeterminate length.
They ask users to wait a moment while something finishes up, without
getting into specifics about what's happening behind the scenes.

Related

About Android Progress Dialog. Avoid?

I was reading about Dialogs in Android site and I came across a section that saying "Avoid ProgressDialog".
Here is the link: http://developer.android.com/guide/topics/ui/dialogs.html
does that means they recommend not to use it? I really need a popup with showing progress while my app is doing background work.
Does that means I have to build my own Progress Dialog using ProgressBar class? How would one build it by the way? Any help is appreciated.
Edit:
With Android O, ProgressDialog is now officially deprecated. An alternative is approach is suggested
This class was deprecated in API level O.
Use a progress indicator such as ProgressBar inline inside of an activity rather than using this modal dialog.
Original answer:
This is all from a design & user interaction perspective, not a code perspective.
The UI guidelines are telling you to avoid using a ProgressDialog not because the class is deprecated (it is not at the time of writing this answer), but rather because it forces the user to avoid interacting with the application and merely stare at the screen.
Take the Google Play app as an example. While it downloads an application/update, you can still swipe, navigate, etc. You can still be involved with the app while it is doing something.
If you absolutely need the user to cease interaction until the progress bar finishes, by all means do so. The docs are merely saying you may be able to find better ways of doing it (hence the link to Progress & Activity).
With ProgressDialog being deprecated in Android O. You should create a ProgressBar and show it by setting it's visibility. I use DelayedProgressDialog from https://github.com/Q115/DelayedProgressDialog It does the same as ProgressDialog with the added benefit of a delay if necessary.
Usage:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");

best practice for "freeze screen" while there are some operation (with progress bar ?)

Im a beginner in Android. I would like "freeze" my app (I mean, the user can't do any action) when I must do some operation.
By example, I delete all row of one my SQLiteBase's table..during this process, I would like show a progressbar and the user can't do anything before the end.
I know (a little) the progressbar mechanism and the AsyncTask but I find it's a heavy solution.. there is a better simpler solution/practice ?
In general, freezing is not a recommended thing and you should always try to allow the user to cancel or go back. It's annoying to wait for operations to finish. More info can be read here.
However, sometimes when you must, you must...
In short, this is how you show a ProgressDialog :
ProgressDialog progressDialog = ProgressDialog.show(this, "dialog title",
"dialog message", false);
(the "false" value is so that the user won't be able to cancel it using the back button)
Then, when you need to dismiss it (upon onPostExecute if you use an AsyncTask), you simply call :
progressDialog.dismiss();

Display loading screen using AsyncTask with ListActivity

I've got an app that uses ListActivity to give users a list of actions. When they click one I use an Intent to launch a separate activity.
My problem is that the actions that the app performs take about 20 seconds to finish, and since I don't want the user to receive that nasty ANR dialog, I tried to use AsyncTask to present them with a loading screen in the mean time. I tried using setContentView(R.layout.loading); on onPreExecute(), but it throws a NullPointerException which as far as I have figured out is due to the fact that loading.xml is not "a ListView whose ID is android.R.id.list".
So what can I do now? How can I show that loading screen? Is there a way around this pretty annoying situation? Any help would be greatly appreciated. Thanks!
I am not sure exactly what your use case is; you have a list of items that are populated immediately, and upon selecting one an action is taken? The action that is taken is to launch another Activity which performs background processing?
Or does it take that long to populate the list of actions?
If the former, you can use an AsyncTask for the long-running activity instead of an Intent to launch another Activity: in the callback you get for the click on the item in question, you would create the AsyncTask, and in doInBackground you would perform the long-running activity, with onPostExecute refreshing or manipulating your list as necessary.
Another thing to consider is using a dialog box to show a loading screen, if the loading is required to happen before you launch a new Activity.
If you can further describe your use case, I can help you more.
It's not the loading screen you need to have on the AsyncTask, it's that 20-second Activity initialization. I would look for a way to do all the setup in a background thread in a Service while the user is free to merrily bop around in other Activities. I'd try hard to find a way not to just stall the user for 20 seconds. Maybe take them to the target Activity and show them data cached from their last visit until the new set is ready.
Fire up and display your loading dialogs in your onCreate() of the Activity being called, then call Dialog.dismiss() in your AsyncTask's onPostExecute().

Delay after pressing a button

After I press a button, Android parse a JSON file and pick the info it needs. Until yesterday, I was using an external library created by a user and it worked perfectly. But now, I don't want to depend on him, so I've searching info about Google's GSON. I've implemented this library with no problem, but now, after pressing the button that opens a new activity there's a delay.
This delay is due to the connection and parsing that are done before the activity shows.
How can I force the app to wait the Internet connection until de Activity is shown? It's a lil bit uncomfortable because after pressing the button, it seems that the app has frozen, but after all data is loaded, the new activity appears.
Thank you in advance!
Use AsyncTask or Handler for network operations. Do never put "long time" operations into the UI thread
Use asyncTask.. and if server communication is a success then show the new Activity or else exit..
it is possibl;e via Handler and AsyncTask see this How to set delay in Android onClick function you will get how to use Handler and see this for Asynctask https://stackoverflow.com/questions/7644567/need-a-simple-example-for-android-asynctask

Changing Progress Dialog Message While Running

I've got a fairly simple issue I'm not sure how to resolve. I want to change the message text of a Progress Dialog while it's running and showing. An example of this would be something like a "time remaining" counter that would count down while the progress animation is spinning. I'm not sure how I would approach this because doing progressDialog.setMessage(String), even within a running thread, will not change anything in the Dialog itself. Thank you!
You might want to check this question out.
In short: You need to create a little Runnable which you can execute on the UI thread using runOnUiThread...

Categories

Resources