Hello, this is momorito. This time, I will summarize the ** procedure for implementing the splash screen ** when creating an Android application.
Possible ways to implement a splash screen
Etc., but this time we will implement it with the former ** Theme replacement type **.
As shown in the gif below, tap the app → display the splash screen → after a few seconds, apply the original theme and aim to display the main activity.
This time I used the image below. It is a png prepared by 160px x 160px. (Other sizes are OK as long as they are displayed.) The file name is splash_logo.png.
Since the image is displayed in the center of the screen this time, set the background color outside the image. Since the background of the image itself was set with ■ # 6A91ED , the same color is defined in color.xml.
color.xml
…
<color name ="background_color">#6A91ED</color>
…
Right-click on the drawable folder → select New → Drawable Resource File to create splash.xml. The name is splash and the root element is layer-lits. Describe as follows in splash.xml.
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Background color setting-->
<item
android:drawable="@color/background_color"/>
<!--Image settings to display-->
<item>
<bitmap
android:gravity="center"
android:src="@drawable/splash_logo"/>
</item>
</layer-list>
This completes splash.xml.
This time, there is themes.xml in res/values, so I will describe it in it. It seems that styles.xml was created in res/values in the past ... Well, as long as you can describe style, there is no problem. Add as follows to themes.xml. In addition, although the original Theme is set for parent, it may be refreshing to set NoActionBar in some cases.
Themes.xml
…
<style name="SplashTheme" parent="Theme.InfoDevice">
<item name="android:windowBackground">@drawable/splash</item>
</style>
…
Apply Theme for splash screen to AndroidManifest.xml. At this time, please remember the original Theme firmly. (In this example, the theme called Theme_InfoDevice was originally applied.)
AndroidManifest.xml
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:theme="@style/SplashTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
under android: name
android:theme="@style/SplashTheme"
I am adding. (If originally stated, rewrite @ style/... below to Theme for the splash screen.)
After starting MainActivity, the theme of the splash screen has been applied, so wait a few seconds and then return to the original theme. The contents to be described in MainActivity are as follows.
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setTheme(R.style.Theme_InfoDevice); //Originally applied theme
setContentView(R.layout.activity_main);
…
}
This completes it.
Recommended Posts