Search code examples
androidlayoutviewsetcontentview

How do I force the layout to be shown right now in android?


I have the following question/problem:

according to my knowledge with setContentView(...) we bring a layout into view. So far so good but at which point will it be shown on the screen?

//quote

public class MainActivity extends Activity {

GlobalDataBase g_db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.create_user_layout);
    
    //here will come the rest of the declaration and
    //a database initialization which is a little time consuming
    //example
   g_db = (GlobalDataBase) getApplicationContext();
   g_db.initialize();

   } 

 // other subroutines and so on

} //point where the layout will be shown on the device

//endquote

Only after the last } ,which defines the end of the activity, the layout will be shown on the display.

Now my question is, if it is possible to force the layout be shown on the display directly after the setContentView(...)

In my program the loading of the database will take a lot of time depending on the internet speed. Sometimes it takes around 20 seconds where the screen is blank.

I want to show a message like "Database loading..." on the screen before I inizialize the database. But that doesnt work because the message is shown only at the end of the } which defines the end of the activity.

Any suggetions?

Thanks for your support


Solution

  • This is happening because you are using DB on main thread. If you do DB initialization on background thread then it will solve the problem.

    You can use Running Android tasks in background threads to implement this DB initalization on background and communicate to main thread when query is complete with result if needed.