In this article, we take a look into creating a splash screen in Android using Xamarin. A splash screen is non different than other screens, that indicates the loading of resources/data for application. Usually most of the real time application have a splash screen as the first screen to show when application starts, and it is automatically goes off once app is fully loaded.
All you have to do is to define your activity theme style inside Resources/values/Styles.xml
file. If you take a look into below style attributes, they are self explanatory. The android:windowBackground
attributes takes the link to splash image.
Styles.xml
<?xml version="1.0" encoding="UTF-8" ?> <resources> <style name="Theme.SplashActivity" parent="android:Theme.Holo.Light.NoActionBar"> <item name="android:windowBackground">@drawable/ic_splash_logo</item> </style> </resources>
SplashActivity.cs
Now let us create a new Activity for splash screen. In this example, we have named it as SplashActivity, and paste the below code.
namespace MyApplication { using System.Threading; using Android.App; using Android.OS; [Activity (Label = "SplashScreen", MainLauncher=true, NoHistory=true, Theme="@style/Theme.SplashActivity")] public class SplashActivity : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Wait for 2 seconds Thread.Sleep(2000); //Moving to next activity StartActivity(typeof(MainActivity)); } } }
In the above code we have specified [Activity]
attribute with following properties
- MainLauncher – This specifies that the activity is a launcher activity and will start automatically when application is launched
- Theme – This specifies the custom theme to android activity
- NoHistory – By default Android application maintain back stack of all activities. When user moves from ActivityA to ActivityB, and presses device back button from ActivityB, android system automatically shows to ActivityA. In case of splash screen, once application is loaded, we are not welling to display splash screen again at any point of time. Hence, we must ask android system to explicitely, not to add SplashActivity into backstack. This can be done by using
NoHistory = true
argument. .
A typical android application Splash screen will perform the loading activity. As this is an example we are just using Thread.Sleep(milliseconds)
to create a delay.