In Android it’s easy to make a splash screen not time based, but showed only until the application is not ready. First of all we have to create a drawable xml file. This file will be the splash screen.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"> <!-- The background color, preferably the same as your normal theme --> <item android:drawable="@android:color/white"/> <!-- Your product logo - 144dp color version of your app icon --> <item> <bitmap android:src="@drawable/product_logo_144dp" android:gravity="center"/> </item> </layer-list>
Then we have to set it into our own theme, in the styles.xml file. For example, like this:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@drawable/splash_screen</item> </style> </resources>
Now you can try to set the theme in the manifest.xml (android:theme=”@style/AppTheme” under the application tag) and launch the application! If you try you can see that the image is still showed as your application background; it’s not very nice. Here you have two ways to restore your normal background.
- Create a second different theme in the styles.xml with the android:windowBackground wanted. Then set the theme in the activity.
public class MyMainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { // Make sure this is before calling super.onCreate setTheme(R.style.Theme_MyApp); super.onCreate(savedInstanceState); // ... } }
- In the activity set the drawable window, maybe useful if it’s a single color.
public class MyMainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); // ... getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); } }
That’s all! Enjoy your splash screen!
L'articolo Android: make a splash screen until the app is ready sembra essere il primo su -Bottyland-.