tags:

views:

728

answers:

2

I have a front screen with a button which opens a second screen. The second screen can take a few seconds to load so I want to display a dialog while loading. My problem is the dialog does not display while loading second screen, but it displays when I return to first page from the second page. If I comment out the "startActivity" to open second page the dialog shows fine. I'm fairly new to android programming - I guess it has something to do with threads.

//code snippet from inside onCreate:
NewGame.setOnClickListener(new View.OnClickListener() {
         public void onClick(View arg0) {
//does not get displayed before 2nd page opens 
          showDialog(DIALOG2_KEY); 
//shows fine if next 2 lines commented out
             Intent i = new Intent(screen1.this, SudukuXL.class);
            startActivity(i);

I've dealt with the dialog showing on returning to the front screen using onPause(). I've tried using threads to seperate the dialog from the startActivity but I've had no luck. Any help would be appreciated.

I used code from Android examples to create dialog. I include below for reference: protected Dialog onCreateDialog(int id) { switch (id) {

       case DIALOG2_KEY: {
           ProgressDialog dialog = new ProgressDialog(this);
           dialog.setMessage("Loading...");
           dialog.setIndeterminate(true);
           dialog.setCancelable(true);
           return dialog;
       }
   }
   return null;

}

A: 

Hello,

IMHO, I'd rather prefer to use ViewSwitcher instead of dialog. But anyway I think you should show dialog (or switch view) in "second screen". Simply let your "second" activity to start and then show dialog in onCreate() while proccessing some stuff, and after finishing just remove dialog(or switch view). My advice is to use AsyncTask or like this to do something in background!!! Also this topic can help you

ponkin
I have tested where delay occurs. It is actually building XML - there are 81 editTexts in a table. In the emulator on my PC - using a blank java class but same xml it takes 2 seconds to display. With the full java class added it takes about 3 seconds. Is it possible to display dialog or ViewSitcher on second screen while building XML? - I will have a go.
darkdusky
So the problem is in time that is taking by emulator to render your view? Am I Right? I don`t know what you are trying to develop but I'd rather use something like Adapter with huge views with many elements. The advantage of Adapters and AdapterView is that you can render just visible part of view and data. check the docs. http://developer.android.com/reference/android/widget/Adapter.html
ponkin
Thanks for advice - I'm doing a suduku game so 81 individual edittexts. Should I abandon my attempt to split the setContentView and showDialog in different threads (see below) ?
darkdusky
I tried GridView as an AdapterView but when I add editTexts into it I get an error: "addView(View,LayoutParams) is not supported in AdapterView" If this is not possible- I'd like to try your original suggestion - load a dialog in the second screen while setContent /XML is built. Have you any idea how I can show the dialog while the original XML is being built - as in posting below.
darkdusky
Actually I have no idea how to show dialog when android is rendering your xml after calling setContentView(). But I have few recomendations: AFAIK having more than 80 views in layout is not recomended, if you try to use special tool "layoutopt" it will warn you about that!(http://developer.android.com/guide/developing/tools/layoutopt.html) So I insist on using Adapters and AdapterViews, But you should write your own implementation of adapter and adapter view try to extend one of standart(GridView...). I will try to write a simple example later.
ponkin
A: 

I've tried displaying a dialog until xml filled but haven't got it quite right. Here is one way I tried:

public void onCreate(Bundle icicle)
           {
             super.onCreate(icicle);
             showDialog(DIALOG2_KEY);

             new Thread(new Runnable(){
                    public void run(){
                         setContentView(R.layout.saved); 
                   }
              }).start();
               //   removeDialog(DIALOG2_KEY);

I tried a few other ways around including swapping what is inside thread and order of execution etc. None of ways works. The above code shows dialog quickly (excellent) but then crashes.