Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Splash Screens in Android: Method 1

Problem:

Most applications require an introductory opening screen.

Solution:

Such introductory screens are called Splash screens. An activity that is finished within a span of 2-3 seconds or dismissed on the click of a button is a splash screen.

Discussion:

The splash screen displays for 2 seconds and then the main activity of the application appears. For this, we add a java class that displays the splash screen. It uses a thread to wait for 2 seconds and then it uses an intent to start the next activity. public class SplashScreen extends Activity { private long ms=0; private long splashTime=2000; private boolean splashActive = true; private boolean paused=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); Thread mythread = new Thread() { public void run() { try { while (splashActive && ms < splashTime) { if(!paused) ms=ms+100; sleep(100); } } catch(Exception e) {} finally { Intent intent = new Intent(SplashScreen.this, Splash.class); startActivity(intent); } } }; mythread.start(); } } The layout of the splash activity, splash.xml is like: width="fillparent" android:layoutheight="fillparent"> width="wrapcontent" android:layoutheight="wrapcontent" /> width="wrapcontent" android:layoutheight="wrapcontent" android:layoutbelow="@id/image" android:layoutgravity="centerhorizontal"> The splash screen looks like: [[Image:splash1.png]] In 2 seconds, this activity leads to the next activity, which is the standard Hello World Android activity : [[Image:splash2.png]]

See Also:

Downloads:

(not handled yet)