Transparent loading activity over another activity - android

I'm looking for the best way to lock all the buttons and fields on my view while the application is waiting for server to respond. I created an activity that will be a little transparent and has a loading bar/spinner on it. The activity stops itself when the respons is received.
Is there any other, better way to do that. All I found on forums and such are 3-4 years old posts that use outdated/deprecated methods.
Any suggestions about how I should lock the activity and let the user know something is going on and that he/she should wait for it to finis...
Thank you

How about just using a ProgressDialog?
pd = new ProgressDialog(context);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
And then when your results come up, just hide the ProgressDialog and launch your new Activity.

Simply you can use Android AsyncTask Class like,
Initially disable all of your views (using View.setEnabled(false))
Check your Server Response (Hope you are using AsyncTask class)
Simply show Progress Dialog (non cancelable) until you can get the response from server (in AsyncTask's onPreExecute() method)
progressDialog = new ProgressDialog(Activity.this);
progressDialog.setCancelable(false);
progressDialog.setMessage("Please Wait...");
progressDialog.show();
Finally, enable your all views within AsyncTask's onPostExecute() once you get Response (using View.setEnabled(true))

Related

How to make a loading screen while doing a huge process

i want to make a loading screen while the android app is doing a huge process like finding prime numbers in a range.it takes a long time for the process to done and i want to appear a loading screen or progress bar while it is doing the huge process. here are the codes of my app:
You can add a simple ProgressDialog.
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading..");
progress.setMessage("Please wait...");
progress.setCancelable(true);
when you need to start it, call
progress.show();
when you need to dismiss it, call
progress.dismiss();

How can I add progressDialog or spinner to already finished app?

I have to update some application. It is already in use, so I don't want to make big changes...
I want to add a progress dialog or a spinner every time when new activity is loaded. I read some articles and questions here like I have to use AsyncTask and so on. But I want to know if there is any easier way. Maybe something like creating some function which will showing spinner/progressDailog until new layout is loaded.
If you know what I want, can you tell me, if it is possible? Thanks
EDIT: I have one idea. Could I create layout with spinner, which would overlay the current layout? If yes, how?
EDIT two: AsyncTask
private ProgressDialog progressDialog;
#Override
protected void onPreExecute (){
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
// Some code here
#Override
protected void onPostExecute(CustomAsyncTaskResult<User> result) {
progressDialog.dismiss();
((LoginActivity)mContext).getResult(result);
}
The simple answer is - no, there is no simple way. If you want to display a spinner while an activity is loading, you need to create an asynctask (or any other separate thread). The good news is that you can create one asynctask and reuse it in all of your activities. The bad new is that you don't have any choice but to modify every one of your activities where you want to add the spinner.
The reason using AsyncTask is suggested is because ProgressBar would otherwise be displayed using the UI thread which you do not want to do as it would block other operations the UI thread would have to do. You need to delegate the ProgressBar display to a background thread. So no, you can't do this.

synchronize method with progress dialog

I have loadData() which used to load my Neuroph network.
I want to run it with a progress dialog but if part of loadData() execute i increase my progress dialog.
I need tutorial for it.
I know Thread with Handler, AsyncTask. but cannot do that with these two methods.
So i recommend to you use meant AsyncTask that is specified for updating UI with some progress. This tool contains three the most important methods:
doInBackground - It's doing your work in background Thread.
onProgressUpdate - For updating your UI(in your case here you can incrementing your progress for ProgressDialog)
onPostExecute - It's called after your work finished.
Then, AsyncTask is strong tool, is very efficient and also using generics so is type-safe. But you need to read more about it so first have look at reference and then i recommend to you have look at Android Threads, Handlers and AsyncTask on Vogella, it's great source.
Then you want to use ProgressDialog.
So simply create it and show() it before you execute AsyncTask. Then in onPostExecute method you just call dismiss method for dismissing ProgressDialog.
ProgressDialog pd = new ProgressDialog(this);
pd.setProgress(0);
pd.setTitle("Some title");
pd.setMessage("Some message...");
pd.show();
yourTask.execute(); // here you are executing your AsyncTask
Then in your OnPostExecute method just call dismiss:
#Override
protected void onPostExecute(Void unused) {
pd.dismiss(); // dismissing your ProgressDialog
}

Android: How to create the Google Plus login animation?

Does anyone know how to create a similar animation to the login animation used in the Google Plus Android app?
Is there something similar in the Android SDK that I can use? Or should I just build it from scratch? I'm interested especially in the fact that the UI behind the modal animation is dimmed and disabled.
Thank you.
Are you taking about that progress dialog spinning thingy that says "signing in"? That's not a custom animation at all, it's a common widget.
Here's the code:
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Signing in...");
progressDialog.show();
//... complete sign in...then
progressDialog.dismiss();
A ProgressDialog done this way, automatically takes care of dimming/blurring the background. You should really read about dialogs: http://developer.android.com/guide/topics/ui/dialogs.html
To show the progression with an animated progress bar:
1- Initialize the ProgressDialog with the class constructor, ProgressDialog(Context).
Set the progress style to "STYLE_HORIZONTAL" with setProgressStyle(int) and set any other properties, such as the message.
2- When you're ready to show the dialog, call show() or return the ProgressDialog from the onCreateDialog(int) callback.
3- You can increment the amount of progress displayed in the bar by calling either setProgress(int) with a value for the total percentage completed so far or incrementProgressBy(int) with an incremental value to add to the total percentage completed so far.
For example, your setup might look like this:
ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
The setup is simple. Most of the code needed to create a progress dialog is actually involved in the process that updates it. You might find that it's necessary to create a second thread in your application for this work and then report the progress back to the Activity's UI thread with a Handler object. If you're not familiar with using additional threads with a Handler, see the example Activity below that uses a second thread to increment a progress dialog managed by the Activity.

AsyncTask onPostExecute not running on main thread?

I have a few Activities on my app that hit a web service. Since I don't want to hold up the main thread, this code is in an AsyncTask. However, I do not want the user to be manipulating the Activity while the web service is being called, so before executing the AsyncTask I show a ProgressDialog which spins and blocks the screen. In the onPostExecute method of the AsyncTask, the first thing I do is dismiss the ProgressDialog.
This should prevent the user from manipulating the Activity without actually blocking the main thread.
However, I've noticed a few times where the ProgressDialog is never dismissed and the user becomes stuck. The AsyncTask has finished, onPostExcute has executed, but the ProgressDialog is still shown. The ProgressDialog has no chance of ever being dismissed and the user is stuck on the screen. Their only option is to visit the Android Settings app and force stop my application.
Does anyone have any idea why this happens? What can I do to fix it?
Relevant code:
This is how I show the ProgressDialog and launch the task:
mProgress = ProgressDialog.show(this, "", "Syncing...", true);
(new MyAsyncTask()).execute(intUserId);
This is the onPostExcute for the task. There is no "#Override"
protected void onPostExecute(Boolean result) {
if (mProgress != null) {
mProgress.dismiss();
mProgress = null;
}
}
Check this:
Make sure you called asynctack.execute() on UI thread.
Use #Override on onPostExcute() to make sure it's properly defined.
Maybe try showing your ProgressDialog in the onPreExecute() Method?
protected void onPreExecute() {
mProgress = ProgressDialog.show(this, "", "Syncing...", true);
}
Give that a shot and see if it works.
Peter Knego posted the following link in a comment under the OP. CommonsWare's solution seems to work well.
Backround task, progress dialog, orientation change - is there any 100% working solution?

Categories

Resources