|
A Crowd-sourced Cookbook on Writing Great Android® Apps |
|
|
|||||||
|
||||||||||
|
Contributed by
Rachee Singh 2011-06-20 10:21:22 (updated 2011-06-22 08:54:03)
In Published Edition?
Yes
|
0
Votes |
Most applications require an introductory opening screen.
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.
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:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:src="@drawable/background" android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/image" android:layout_gravity="center_horizontal"> </ProgressBar> </LinearLayout>
The splash screen looks like:
In 2 seconds, this activity leads to the next activity, which is the standard Hello World Android activity :
|