Tae-Sung Shin
Tae-Sung Shin

Reputation: 20643

How to show splash image while loading activity

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

Upvotes: 6

Views: 12667

Answers (3)

Dr.jacky
Dr.jacky

Reputation: 3547

use AsyncTask

put splash in onPreExecute()

and do your work in doInBackground()

and close splash in onPostExecute()

Upvotes: 7

K.Muthu
K.Muthu

Reputation: 1252

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;
}    

Upvotes: 0

Maneesh
Maneesh

Reputation: 6128

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();
        }
}

Upvotes: 0

Related Questions