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

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.

Related

Fragmenttabhost performance is slow?

I have use the v4 support lib for FragmentTabHost
The requirement is that when I am switching tab one to another & another one, that is calling
onCreateView() & onActivityCreated() every time.
That's why my code performance is slow.
So, any other solutions? how to increase performance in fragment tab?
Sounds like a design smell.
Redesign your code so that heavy work is done asynchronously. Fragments should be able to be built quickly. If there is any large processing that needs to be done to in order for a Fragment to display useful information, that work should be done beforehand or asynchronously after the Fragment is created and the Fragment should be notified to update its content when the work is complete.
First thing which you should take care of is to watch about calculations / loading a big set of data should be places on a different worker thread than main UI thread. The best option to do that (in my opinion) is to use AsyncTask. You can use something like this in your Fragment :
private class LoadData extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute(){
super.onPreExecute();
// this is the place where you can show
// progressbar for example to indicate the user
// that there is something which is happening/loading in the background
}
#Override
protected void doInBackground(Void... params){
// that's the place where you should do
// 'the heavy' process which should run on background thread
}
#Override
protected void onPostExecute(Void result){
super.onPostExecute();
// you should update your UI here.
// For example set your listview's adapter
// changes button states, set text to textview and etc.
}
}
This is the way you can make your tabs work faster.Hope this will help you! : )
I found a solution for that. I inserted all websevices & database transaction code in on create. because oncreate in not calling every time untill the ondestroy not call. & the other one solution is also available we can use
fragment.show();
& fragment.hide(); method
As an addition to Android-Developer: if you already are using AsyncTask, remember that even when you use multiple AsyncTask's, they are executed in the background, but all sequentially! If you want more threads to handle your tasks, check out this post, which perfectly explains how to achieve that! Running multiple AsyncTasks at the same time -- not possible?

How to move the heavy sorting work away from UI thread

I have a sort of numeric keypad and a list. When the user chooses the keypad, I call a function to sort the list. The user can press the button to show the list at any time.
But that list can be pretty large, 500+, so I was thinking about doing the sorting no longer on the UI thread.
What is the best way to do this ?
Should I use a regular thread, asynctask?
My only worry is that the user can also click the button to show the list while the asynctask is not finished yet. How should I handle that ?
Thx
Definitely go for AsyncTask which is designed for heavy work outside UI. Concerning your last question, make button disabled and enable it in AsyncTask's onPostExecute(). Cheers.
Definitely you need a asynctask. In your onCreate method hide all of your buttons and textviews. Before you do something in the background you need to show user a load bar or a spinner so that user can't click any of the buttons you have created. Here is a sample:
class LoadAllProducts extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Academic.this);
pDialog.setMessage("Loading. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
//Do something
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
//finally show your button
}
});
}
Before using this code make sure you have declared the pDialog. In my program I had used spinner.
AsyncTask looks good for this... task. You can also make some sort of state-member to check whether the sort task is running and depending on it start the task or do nothing when the user presses the button. As an alternative, if something has changed, you can also cancel the task and start a new one.
Go for Async Task and you can show some progress bar in onPreExecute() and stop it in onPostExecute().
User wont be able to clcik anything till progress bar is running on screen.
You can use AsyncTask as other have told or you can create a thread and run long running operations and update UI accordingly with handlers. Have a look at this link http://www.vogella.com/articles/AndroidPerformance/article.html

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.

Loading Page in android

How can I make loading page in android? I want it when my application starts.
Like the loading pages in games?
Could You help me with that?
There are loads of examples in the internet, search by "splash screen"
http://www.anddev.org/viewtopic.php?t=815
One way of doing it would be to set your contentview in onCreate() then do all the loading in an async task and when that finishes load the 'real' layout in the onPostExecute. Check out http://developer.android.com/reference/android/os/AsyncTask.html
That way you load your "real" layout when it actually finishes loading rather than picking a generic time to switch views. This is of course assuming that you want a loading page and not a splash screen. If you want that, checkout the other answers.
Here's a quick example... Say you have a file called Hello.java .. You'd set your content view to your loading layout in OnCreate() then call this class with something like.. new DownloadFilesTask().execute(); put this private class in it...
private class DownloadFilesTask extends AsyncTask<String, String, String> {
protected Long doInBackground(String... params) {
//grab stuff from the server, compute pi to 100000 places etc.
}
public void onPostExecute(String result) {
//this will now switch us to our real layout, you can now do all your fancy UI stuff! :)
setContentView(R.layout.reallayout);
}
}
Basically this is multithreading the easy way. onPostExecute runs back on the UI thread (your main one) while doInBackground does everything on a separate thread so no black screens! I'd really urge you not to use Runnable in this situation.
You mean a splash screen right? a simple google search would reveal a lot :)
This piece of work helped me to make a splash screen.
Rather than copy and pasting the code, try to understand how he uses Threading with a time limit to achieve this target.

Android: Loading data and then notifying an Activity? Also documented a failed approach!

I just tried a stupid approach and it crashed my app... Basically I have an activity that has three tabs (containing three activities). Each of the tabs gets its input from an xml file downloaded off the net. Everything is fine but when I start my app, it has download the xml file and there's a "wait" time for this.
I managed to get around this by adding a splash screen. It looks beautiful but the problem is when I click on the second tab, it still has to get the list off the net, so it looks ugly now... It waits before displaying the list. So what I did was design an AsyncTask that just downloads the xml file. In my main activity, I spawn two tasks initially and send the URL and Intent as parameters. And inside the acitivities that start inside the tabs, I use a wait(). Inside the AsyncTask, after it is done with the download, I notify the Intent using notify(). This crashed! Of course, I didn't expect it to work but just wanted to try :) Writing it so that I can either get a feedback as to why this failed or to prevent others from wasting their time on this...
Now, I am sure many face the problem of a "wait" time inside the tabs. How do I solve this? I am thinking of either dimming the screen and then displaying a series of toasts or display a progress indicator inside the tabs or pre-fetching the xml files... I don't have a clue on how these can be achieved... Any thoughts?
Credit: To Mark. Thanks!
Problem: Display a Progress Indicator when your application is busy doing some work
Approach:
public class Approach extends ListActivity {
ProgressDialog myProgressDialog = null;
ListView myList = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = getListView();
myProgressDialog = ProgressDialog.show(getParent(),
"Please wait...", "Doing extreme calculations...", true);
//Do some calculations
myProgressDialog.dismiss();
}
}
There are a few challenges (like updating some UI elements). You might want to spawn a different thread to do your calculations if needed.
Also, if you are interested in this, you might be interested in Matt's approach too: android-showing-indeterminate-progress-bar-in-tabhost-activity
ProgressDialog.
Or, make the tabs have android:visibility="gone" until such time as the data is ready, then make them visible. In the interim, show some sort of loading graphic (perhaps with a RotateAnimation applied).

Categories

Resources