views:

743

answers:

3

I am using below code where , i want to show dialog in front and loading content in background but not able to do the same . Please advise.

dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
runOnUiThread(new Runnable(){
  public void run() {
    setContentView(R.layout.main_layout);
    dialog.dismiss();
  }
});
A: 

There's an example of how to do loading in a separate thread on the Android developer's website. To see the actual code, expand the view at the end of the section on Progress Dialogs.

However, for your particular situation, you need to rework how you're doing things. What is taking so long that you need a progress dialog for it? You should load that in the 2nd thread, while displaying some kind of temporary layout with setContentView. Then once the thread finishes loading, either call setContentView again, or change the text, images or whatever that you loaded with the thread.

Lastly: If you're really new to programming in Android, I'd avoid trying to use separate threads and complicated loading things for now. It's a bit tricky to do and there's a lot to understand first.

Steve H
Actually my first activity is calling second activity which loads data in Oncreate method which is taking to much time. So instaed of calling second activity using Intent, i want to simply change the content and load the relevant data in thread by showing progress bar.
Maneesh
A: 

Sounds like you want a splash screen or something but setting the content view should not be done in a thread. Post the layout.xml, what is in there that is taking it so long to render.

BrennaSoft
I created two activities, first activity is calling second one which loads data, change properties of controls etc in its Oncreate(). So instead of calling second one from first, At the end of first, i simply change layout of first one and loads data using thread by showing progress dialog. But niether i am able to change layout in thread class nor i am able to load data into controls in thread. Any suggestions?
Maneesh
A: 

I got solution by reading below link and implemented code as below: http://developer.android.com/guide/appendix/faq/commontasks.html#threading

int glb=0,glbtotal=3;
final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
    public void run() {
        updateResultsInUi();
    }
};

public void open_next_screen() { dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true); startLongRunningOperation(); }

private void updateResultsInUi() {

    // Back in the UI thread -- update our UI elements based on the data in mResults
    switch(glb)
    {
    case 0:
         setContentView(R.layout.main_layout);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

        break;
    case 1:
    part2();

    break;
    case 2:
     part3();

    break;
    }

}

protected void startLongRunningOperation() {

    // Fire off a thread to do some work that we shouldn't do directly in the UI thread
    Thread t = new Thread() {
        public void run() {


            for(glb=0;glb<glbtotal;glb++)
            {
                mHandler.post(mUpdateResults);

            switch(glb)
            {
            case 0:
                part1();
                break;

            }
            }
              dialog.dismiss();
        }
    };
    t.start();
}
Maneesh