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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<strong><?xml version="1.0" encoding="utf-8"?></strong> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/black"/> <item> <bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/> </item> </layer-list> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<resources> <em> <!-- Base application theme. --></em> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <em> <!-- Customize your theme here. --></em> </style> <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowBackground">@drawable/background_splash </item> </style> </resources> |
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.
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:
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
and GitHub:
I blog frequently and I truly thank you for your content. The article has truly peaked my interest. I’m going to take a note of your website and keep checking for new details about once per week. I subscribed to your Feed too.|