string.xml
1
2
3
4
5
6
|
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SplashScreen</string>
<string name="main_screen">MainScreen</string>
<string name="splash_screen">SplashScreen</string>
</resources>
|
main.xml
1
2
3
4
5
6
7
8
9
10
11
12
|
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_screen"
/>
</LinearLayout>
|
splash.xml
1
2
3
4
5
6
7
8
9
10
11
12
|
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/splash_screen"
/>
</LinearLayout>
|
The layouts aren’t beauty but thats not the intention of this how to.
The activity MyApp is really nothing more than a new created activity.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.droidnova.android;
import android.app.Activity;
import android.os.Bundle;
public class MyApp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
|
But now lets go to the more interesting part: the SplashScreen activity
itself.
We will work with a thread to realize the function, that the screen will
disappear after some seconds. For that we need two class variables. One
for the number of time the screen has to appear and the flag, if the thread
should run or not.
1
2
|
protected boolean _active = true;
protected int _splashTime = 5000; // time to display the splash screen in ms
|
In the onCreate() method, we will create a Thread object and override
the run() method dynamically.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
startActivity(new Intent("com.droidnova.android.splashscreen.MyApp"));
stop();
}
}
};
splashTread.start();
}
|
As you see from line 13 – 18, we run in a while loop if our flag is true
and if our waited variable is less the number we defined in our variable
_splashTime.
Finally, we finish our activity, which prevents the restart with the back
button, start a new activity which will start our MyApp activity and the
last step should be to stop the thread.
At the end we override the function onTouchEvent() and set the _active
flag to false to force the thread to stop the while loop and start the MyApp
activity immediately.
1
2
3
4
5
6
7
|
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_active = false;
}
return true;
}
|
Thats it!
Full Eclipse project:
SplashScreen.zip
How to use custom de...
lets say i change this app to days to...
How to display a JPG...
Is that posible to zoom in and drag p...
Making a custom Andr...
Cool - I will use it carefully i promise
How to display a JPG...
problem in image loading - i have exe...
Introducing Calculon...
Thank You for your contribution - Hi,...