top of page

Splash Screen – faster way

  • agulevski10
  • Aug 21, 2018
  • 2 min read

Splash Screen creating from another perspective

The other way of creating the Splash Screen is more fast and without using messy layout and splash screen activity files (.xml & .java)

This is a simple example how this is implemented:

First you don’t need to create a separate layout file for the activity, just the theme. Specify your splash screen’s background as the activity’s theme background. To do this, first create an .xml drawable in res/drawable with this code inside. This is used for setting background color (@color/black) & image (@mipmap/ic_launcher where this is the app icon).

<strong>&lt;?xml version="1.0" encoding="utf-8"?&gt;</strong> &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@color/black"/&gt; &lt;item&gt; &lt;bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/&gt; &lt;/item&gt; &lt;/layer-list&gt;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<strong>&lt;?xml version="1.0" encoding="utf-8"?&gt;</strong>

&lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt;

&lt;item android:drawable="@color/black"/&gt;

&lt;item&gt;

&lt;bitmap

android:gravity="center"

android:src="@mipmap/ic_launcher"/&gt;

&lt;/item&gt;

&lt;/layer-list&gt;

You must set this in your main app theme located in res/values/styles.xml and In your new Splash Theme, set the window background attribute to your XML drawable (@drawable/background_splash) with this code:

&lt;resources&gt; <em> &lt;!-- Base application theme. --&gt;</em> &lt;style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"&gt; <em> &lt;!-- Customize your theme here. --&gt;</em> &lt;/style&gt; &lt;style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"&gt; &lt;item name="android:windowBackground"&gt;@drawable/background_splash &lt;/item&gt; &lt;/style&gt; &lt;/resources&gt;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

&lt;resources&gt;

<em> &lt;!-- Base application theme. --&gt;</em>

&lt;style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"&gt;

<em> &lt;!-- Customize your theme here. --&gt;</em>

&lt;/style&gt;

&lt;style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"&gt;

&lt;item name="android:windowBackground"&gt;@drawable/background_splash &lt;/item&gt;

&lt;/style&gt;

&lt;/resources&gt;

In your Splash Screen .java file just add this code for automatic forwarding to your Home or Main or whatever you like – Activity .java

You’ll need in onCreate() to make new Intent from THIS activity to, lets say, HomeActivity.class (designated activity must be declared with .class extension)

The method startActivity() is for starting new activity, but it must have a intent parameter that we defined before

And finally finish() method for ending SplashScreen activity so it can be viewed when back button is pressed.

<strong>public</strong> <strong>class</strong> <strong>SplashScreen</strong> <strong>extends</strong> AppCompatActivity <strong>{</strong> @Override <strong> protected</strong> <strong>void</strong> <strong>onCreate</strong><strong>(</strong>Bundle savedInstanceState<strong>)</strong> <strong>{</strong> <strong> super</strong><strong>.</strong>onCreate<strong>(</strong>savedInstanceState<strong>);</strong> Intent intent <strong>=</strong> <strong>new</strong> Intent<strong>(</strong><strong>this</strong><strong>,</strong> HomeActivity<strong>.</strong>class<strong>);</strong> startActivity<strong>(</strong>intent<strong>);</strong> finish<strong>();</strong> <strong> } </strong><strong>}</strong>

1

2

3

4

5

6

7

8

9

<strong>public</strong> <strong>class</strong> <strong>SplashScreen</strong> <strong>extends</strong> AppCompatActivity <strong>{</strong>

@Override

<strong> protected</strong> <strong>void</strong> <strong>onCreate</strong><strong>(</strong>Bundle savedInstanceState<strong>)</strong> <strong>{</strong>

<strong> super</strong><strong>.</strong>onCreate<strong>(</strong>savedInstanceState<strong>);</strong>

Intent intent <strong>=</strong> <strong>new</strong> Intent<strong>(</strong><strong>this</strong><strong>,</strong> HomeActivity<strong>.</strong>class<strong>);</strong>

startActivity<strong>(</strong>intent<strong>);</strong>

finish<strong>();</strong>

<strong> }

</strong><strong>}</strong>

This way is faster because it uses app them as a Splash Screen style, not running the activity layout, so if you initialize something the layout will be delayed after the initialization is implemented and that is not good.

For further research on the Splash Screen:

and GitHub:

Comments


bottom of page