I would like to implement a SplashScreen in my app. I found the best and easiest way is to launch an activity that shows a layout with an image view at the launch of the app and then adding android:noHistory="true" attribute to the manifest.
Now, how do I set the splashscreen activity to launch the MainActivity class after a certain amount of time? Lets say 2 seconds?
This is my splashscreen activity
public class SplashActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
}
}
use
handler.postDelayed(runnable, delayinmilliseconds(2000 in your case));
final Runnable runnable = new Runnable()
{
public void run()
{
//start the new activity here.
}
};
Here is a complete sample.
package com.test.splash;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
public class splash extends Activity {
private static final int STOPSPLASH = 0;
//time in milliseconds
private static final long SPLASHTIME = 3000;a
private ImageView splash;
//handler for splash screen
private Handler splashHandler = new Handler() {
/* (non-Javadoc)
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//remove SplashScreen from view
splash.setVisibility(View.GONE);
break;
}
super.handleMessage(msg);
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
splash = (ImageView) findViewById(R.id.splashscreen);
Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}
}
public class TrackMyMoneyActivity extends Activity {
//member fields
private ProgressBar pbar = null;
private TextView counter_txt = null;
Thread splash_thread = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pbar = (ProgressBar) findViewById(R.id.splashpbar);
counter_txt = (TextView) findViewById(R.id.countertxt);
//define thread
splash_thread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
for( i=0;i<100;i++){
pbar.setProgress(i);
// counter_txt.setText(i+" %");
try {
splash_thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(i==100){
Intent intent = new Intent(getBaseContext(), LoginApp.class);
startActivity(intent);
}
}
});
splash_thread.start();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
I hope it will be solved your solution.
You can also use java.util.Timer
in this way:
new Timer().schedule(new TimerTask(){
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
}, 2000 /*amount of time in milliseconds before execution*/ );
public class Splashscreen extends Activity
{
private static final int SPLASH_TIME = 10 * 1000;// 3 seconds
Button logo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splashscreen);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
try {
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(Splashscreen.this,MainActivity.class);
startActivity(intent);
Splashscreen.this.finish();
//overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}, SPLASH_TIME);
new Handler().postDelayed(new Runnable() {
public void run() {
}
}, SPLASH_TIME);
} catch(Exception e){}
// METHOD 1
/****** Create Thread that will sleep for 5 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 5 seconds
sleep(50*1000);
// After 5 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),MainActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
background.start();
}