1

I would like to create a "loading" screen until the layout of the activity is fully loaded, is there any way to do this?

Or define a custom default background to current theme?

Felipe Porge Xavier
  • 2,236
  • 6
  • 19
  • 27

2 Answers2

0

You should not block Your UI thread.It seems to me that you are doing so and you might get ANR(application not responding exceptions).If you are doing updates on UI thread and those are heavy and time taking actions then you must use worker threads.Worker threads update UI thread asynchronously without blocking it.

The example:

class workerThread extends Thread {
        public void run() {
                    final View play = getActivity()
                            .findViewById(R.id.play);
                    play.post(new Runnable() {
                        public void run() {
                        //put all your time taking tasks inside this post runnable.Here you may implement updateprogressbar etc
                            play.performClick();
                        }
                    });
                }   
        }
    workerThread = new workerThread();
    workerThread.start();

You might consider using AsyncTask too which handles most of the boilerplate code on its own.

You may change the background as you wish but your application will not be stable. If you insist on doing that just create a ProgressDialog in your view and update it as you suit:

Example:
mProgressDialog = new ProgressDialog(getActivity());
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setTitle(R.string.progress_dialog_loading);
    mProgressDialog.setCancelable(true);
    mProgressDialog
            .setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                    mLoadingKeepGoing = false;
                }
            });

    mProgressDialog.show();

Threads and processes are rather complicated topics.Y ou need to study and practice alot. Here are some resources i found for you:

1.Processes and threads for android doc

2.Worker thread blog post

3.Another blog post

4.On stackoverflow

Community
  • 1
  • 1
Mehdi Fanai
  • 4,021
  • 13
  • 50
  • 75
0

Design your activity with two views.

-Loading layout.
-Second layout. 

Now in xml set the second layout visibility to "gone" or "invisible"

Now after your second layout is loaded.. (use an async task, that will do the loading in the back ground. am using pseudocode):

  class Task extends AsyncTask<Void,Void,Void>{
  doInBackGround(){
   Load your content
  }
  onPostExecute(){
  set the visibility here;
  }

set the Loading Layout to "invisible" and the second layout as visible.

You can add animation between views to make it look better.

amalBit
  • 12,041
  • 6
  • 77
  • 94