Creating a progress bar while loading the UI - android

i have created a android app it works fine and I am loading my UI that contains a scroll tab with several fragments, the UI in the main activity takes 4-6 seconds to load and only a blank white screen is seen till then, how can I show a progressbar till that UI has finished loading there s no data to load just the UI so please dont tell about asynctasks

You will have to first display a simple activity with your progress bar and then start to add the parts of your ui in background one by one to the activity by using an async task.
If you are using commands that can only be performed by the ui thread you have to post them to a view or a handler from your async task.
Ensure that the different blocks you are loading are not taking too long to give the ui thread time to update your progress bar in between.

I know you don't want to use an Async task but that would be the best solution. Start an async task loading the UI (Can use existing class) and read a true false (indicating when done) value from onProgressUpdate(), which can be accessed from the UI thread. Display a spinner while false. Hope this helps.

Related

Android Layout not showing

When my quiz app starts for the first time it needs to add questions to a database and a few other things and this takes a few seconds. I want to display a indeterminate progressbar whilst this occurs. Once the questions are added, the class calls finish() and moves on to the main screen. The problem is that the layout isn't drawn unless finish isn't called. I assume it doesn't draw the XML layout until after the oncreate and onstart are called. So how do I get around this?
The reason, the UI isn't shown while you're filling your DB is probably that you're doing that in the UI-Thread - the main thread of your application. As the thread is busy filling your DB, there is no time to show the UI, animate the progress indicator or react to user actions.
Thus you should always run time-consuming tasks in a separate thread. See this excellent entry in the android devloper's blog about painless threading for more information and to do it right:
http://android-developers.blogspot.co.at/2009/05/painless-threading.html
Use AsyncTask, display progress dialog in its onPreExecute and dismiss in onPostExecute prepare your db in doInBackground.
You would have to handle device rotation unless you are using DialogFragment to display your dialog.

UI Pattern for loading images from a background thread

I have code that reloads images via HTTP from the main thread and displays them, right after setting up the layout.
This is now deprecated since network access must no longer be done from the main thread.
At first, I changed it to load the images from a a background thread and then update the ImageViews. But, the user experience changed since the images now "pop out."
Is it possible to make the images load seamlessly?
Or, should I display a progress bar or status message while the images reload.
Seamless loading would require to delay to display your entire ui which is probably not what you want. You could use an animation to fade in your images once they are loaded.

Adding Progress bar in Global application in android

I would like to load progress bar when loading the application. I have 60 sound files which I have put in global application using sound pool. When I launch the application the application goes blank and it takes 30 to 40 seconds to load.
Instead of blank screen is it possible to put some progress bar with a background image until the application loads all the sound files?
I found out the global application doesn't support progress bar or background image? Is this true?
If not can somebody help me out in the above?
Thanks!
Load your sound files in an AsyncTask or a Thread.
You can set an OnLoadCompleteListener to know exactly when loading finishes.
If I'm right, and you mean your Application class, than you can't show a progressbar from there. But you have to have at least one activity, and you can show progressbars in your activity.
Also, you shouldn't make heavy operations on the UI thread such as this. Load the sounds in an AsyncTask, show the progressbar in the onPreExecute() method and hide it in the onPostExecute() method.

setContentView taking long time (10-15 seconds) to execute

I have a large activity that contains 100 or more buttons. But it's working fine once loaded. Problem however is loading. From clicking its launch icon to getting the first view it takes 10-12 seconds. Until the first view, it shows gray title bar in black background.
At least, I want to show a simple progress bar or dialog while its loading. But it seems like you cannot show anything before setContentView executed. I think I have tried everything I could without any success. If you can give me any hint or idea, I would be thankful.
UPDATE:
I found a dramatic resolution. It takes now a second to load the view. I didn't use splash, thread or async task at all - BTW, don't try to use thread or async on UI because Android UI is not thread-safe. Problem was that those buttons were based on a custom class that requires initialization to load same resource. - so 100 or more file operations were happening on setContentView. Making them a just single loading solved my problem.
You are loading data on same UI thread , so nothing will be desplayed during the time of loading .
Use Async Task for loading in separate thread.
1)Show a progressBar in onPreExecute()
2)load data in doInBackground() . no UI related stuff here
3) Update changes on UI ,hide progressBar in onPostExecute()
Use this code before setContentView() is called. Maybe it helps.
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
setProgressBarVisibility(true);

Splash screen while loading resources in android app

I'd like to have a splash screen while loading resources (images and sounds). How do I know everything is loaded?
Are all resources loaded at app startup?
Thanks
For accordingly implementing a splash screen in Android you want to:
Show a foreground screen with some progress indication for the user.
Execute a background thread for doing tasks that take some indefinitive time.
Both threads communicating between them, as you need the foreground to show the progress on the background.
Correctly kill the background thread when it finishes doing it's task. If you are planning to use AsyncTask in Android you have an issue there. (Link)
I've found this tutorial and I strongly suggest it:http://www.41post.com/4588/programming/android-coding-a-loading-screen-part-1
Part 1 accomplish this basic task, part 2 shows you how to correctly kill the AsyncTask. And part 3 puts a customized view in the foreground instead of the ProgressActivity.
You could do all your loading in an asyncTask then your onPostExecute remove the splash screen. This would help ensure that you don't block the UI thread while doing any expensive tasks that could cause an ANR popup.
Here you go, wrote a tutorial on how to create a SplashScreen with a progress bar:
http://blog.blundellapps.com/tut-splashscreen-with-progress-bar/
Basically, instead of your thread it starts an ASyncTask, you pass a reference to your progressSpinner into the ASyncTask and this will update it as the thread is downloading resources (or whatever you want to do).
Here is a complete tutorial on how to get it done. I've used this one myself with great results.
http://www.barebonescoder.com/2010/04/a-simple-android-splash-screen/

Categories

Resources