Splash Screen with no background tasks example
- agulevski10
- Dec 12, 2018
- 3 min read
We all know that Splash screens in Android are very useful if there is some starting background operation that user doesn’t need to see, but takes time to load (for example if the user is logged in or not, some reading of content, checking location, permissions,,,). In this scenario, the Splash screen comes in handy. Almost every time there is a logo, or simple text of the application. After all background operations are done, the application takes you inside.
But what happens if there is no background operations at all? Application that is not connected to the internet, or simple app that does everything in it self, but we want to add Splash at app start?
Here is the thing. First we declare an SplashActivity that is launcher, and we declare it in the AndroidManifest:
XHTML
<activity android:name=".SplashActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
1
2
3
4
5
6
7
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Then we build our SplashScreen activity like this:
public class SplashActivity extends AppCompatActivity { Handler handler; Runnable runnable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); delay(); } private void delay() { handler = new Handler(); runnable = new Runnable() { @Override public void run() { // Start next activity here HomeActivity.start(SplashActivity.this); finish(); } }; handler.postDelayed(runnable, 2000L); } @Override protected void onPause() { handler.removeCallbacks(runnable); super.onPause(); } }
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
30
31
public class SplashActivity extends AppCompatActivity {
Handler handler;
Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
delay();
}
private void delay() {
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
// Start next activity here
HomeActivity.start(SplashActivity.this);
finish();
}
};
handler.postDelayed(runnable, 2000L);
}
@Override
protected void onPause() {
handler.removeCallbacks(runnable);
super.onPause();
}
}
We declare Handler and Runnable at first and we will use them in our delay() method. A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own. In our example we are going to use it with Runnable. The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run. In this run method, we start the next activity, which in our case is Home activity, and we call finish() on Splash screen, so it can be executed only once on every app start, and it cannot be available anymore in case back is pressed on HomeActivity.
After this is made, we call postDelayed() method with our two arguments, previously declared runnable and long value in milliseconds. This time tells us that in 2000 milliseconds the operation declared in the run() method will be executed. Simple, the splash screen will remain 2 seconds on screen and after that the Home Screen will be displayed.
We need to override onPause() method to remove handler callbacks with our runnable, so in that 2 seconds time, if back or home are pressed the app wont go in Home Screen.
Here is the XML layout file for the Splash Activity Screen:
XHTML
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SplashActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Splash Activity" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Splash Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The code for Home Activity Screen:
Java
public class HomeActivity extends AppCompatActivity { public static void start(Context context) { context.startActivity(new Intent(context, HomeActivity.class)); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); } }
1
2
3
4
5
6
7
8
9
10
11
12
public class HomeActivity extends AppCompatActivity {
public static void start(Context context) {
context.startActivity(new Intent(context, HomeActivity.class));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
And the XML layout for Home Activity Screen:
XHTML
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".HomeActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Home Activity!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home Activity!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
This is the style used:
XHTML
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
1
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">





Comments