6

I have an activity that contains many UI views. In its onCreate method, I found single line of setContentView takes 8-12 seconds to be complete. So I want to show my logo image while it's loading. I tried many things but without any success. I suspect main reason might be that before finishing setContentView, nothing can be shown.

Any help would be appreciated.

UPDATE:

I think many people do not know that you cannot show any dialog before finishing setContentView. So using another splash activity does not help me at all.

UPDATE2

I forgot to update this question after I found cause of the problem. Please refer to following question: setContentView taking long time (10-15 seconds) to execute

Community
  • 1
  • 1
Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • I would suggest you my approach: [Link][1] It works fine for me. [1]: http://stackoverflow.com/questions/7321665/custom-splash-screen-to-load-variables-in-android – Zappescu Nov 18 '11 at 08:14

3 Answers3

7

use AsyncTask

put splash in onPreExecute()

and do your work in doInBackground()

and close splash in onPostExecute()

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
0

Below is the simple code for creating splash screen using CountDownTimer class

public class SplashDialogActivity extends Activity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout);
      counter.start();
    }
    MyCount counter = new MyCount(5000, 1000);
 public class MyCount extends CountDownTimer{
            public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            }

            @Override
            public void onFinish() {
                go_back();
            }

            @Override
            public void onTick(long millisUntilFinished) {



              }
     }

 public void go_back()
        {
          counter.cancel();

                    Intent i=new Intent(this,account.class);
                    i.putExtra("first_time", true);
                    startActivity(i);

            this.finish();
        }
}
Maneesh
  • 6,098
  • 5
  • 36
  • 55
  • Sir, you can reduce the time MyCount counter = new MyCount(2000, 1000); 2000 denotes 2 seconds..so this is how you can control the time of splashscreen displayed – Maneesh Nov 18 '11 at 08:45
  • try to display both screen (splash & main) in one activity by using setContentView two times and between them you can use either thread or timer – Maneesh Nov 18 '11 at 09:02
  • one more thing if setContentView is taking time then try to load data or image files not at design time but at runtime – Maneesh Nov 18 '11 at 09:06
0

try this code for splash page

private Thread mSplashThread;    

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.splesh);

    final Splash sPlashScreen = this;   

     mSplashThread =  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    wait(5000);
                }
            }
            catch(InterruptedException ex){                    
            }

            finish();

            Intent intent = new Intent();
            intent.setClass(sPlashScreen,Login.class);
            startActivity(intent);
            stop();                    
        }
    };

    mSplashThread.start();        
 }

// Processes splash screen touch events
@Override
public boolean onTouchEvent(MotionEvent evt) {

     if(evt.getAction() == MotionEvent.ACTION_DOWN)
     {
         synchronized(mSplashThread){
             mSplashThread.notifyAll();
         }
     }
     return true;
}    
zessx
  • 68,042
  • 28
  • 135
  • 158
K.Muthu
  • 1,232
  • 1
  • 17
  • 38