Creating Splash Screen For Android App in Xamarin

A Splash Screen is an image that appears when an application is loading. Splash Screens are typically used to notify the user that the program is in the process of loading. In this article, we will learn how to create a Splash Screen for an Android Application in Xamarin / Mono for Android. In my previous article, we saw how to create a Hello World Application in Xamarin. Before starting, I suggest you read my previous article.
Let's Begin
Step 1
 
Add two images (icon.png for the icon and splash.png for displaying the image on the Splash Screen) in Resources\Drawable folder.
 
Step 2
 
Right-click on the Values folder present in the Resources folder of the project then go to Add -> New item.
 
Step 3
 
Select XML File and provide it the Name Styles.xml. In an Android Application, we can assign themes to an Activity.
 
 
The following is the Styles.xml code: 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <resources>  
  3.   <style name="Theme.Splash" parent="android:Theme">  
  4.     <item name="android:windowBackground">@drawable/splash</item>  
  5.     <item name="android:windowNoTitle">true</item>  
  6.   </style>  
  7. </resources>  
In the code above, we have created a Theme named Theme.Splash and set the window background to the splash.png image present in the Drawable folder.

We have also disabled the title bar by setting windowNoTitle to true.
Step 4
 
Add a new Activity. Right-click on the project then go to Add -> New Item.
 
Select Activity and provide it the name SplashScreen.cs.
 
The following is the SplashScreen.cs code: 
  1. using Android.App;  
  2. using Android.OS;  
  3. using System.Threading;  
  4.   
  5. namespace Splash_Screen  
  6. {  
  7.     //Set MainLauncher = true makes this Activity Shown First on Running this Application  
  8.     //Theme property set the Custom Theme for this Activity  
  9.     //No History= true removes the Activity from BackStack when user navigates away from the Activity  
  10.     [Activity(Label="Splash Screen App",MainLauncher=true,Theme="@style/Theme.Splash",NoHistory=true,Icon="@drawable/icon")]  
  11.     public class SplashScreen : Activity  
  12.     {  
  13.         protected override void OnCreate(Bundle bundle)  
  14.         {  
  15.             base.OnCreate(bundle);  
  16.             //Display Splash Screen for 4 Sec  
  17.             Thread.Sleep(4000);  
  18.             //Start Activity1 Activity  
  19.             StartActivity(typeof(Activity1));  
  20.         }  
  21.     }  
  22. }  
In Activity1.cs, we have added a Button with Id="btn_Hello" from the ToolBox and added a Click Event and Display Toast message on the clicking of the button.

The following is the Activity1.cs code: 
  1. using System;  
  2. using Android.App;  
  3. using Android.OS;  
  4. using Android.Widget;  
  5.   
  6. namespace Splash_Screen  
  7. {  
  8.     [Activity(Label = "Hello World App")]  
  9.     public class Activity1 : Activity  
  10.     {  
  11.         Button btn_Hello;  
  12.         protected override void OnCreate(Bundle bundle)  
  13.         {  
  14.             base.OnCreate(bundle);  
  15.             //SetContentView  
  16.             SetContentView(Resource.Layout.Main);  
  17.             //Get btn_Hello Button control from the Manin.axml Layout.  
  18.             btn_Hello = FindViewById<Button>(Resource.Id.btn_Hello);  
  19.             //Creating Click event of btn_Hello  
  20.             btn_Hello.Click += btn_Hello_Click;  
  21.         }  
  22.         //btn_Hello Click Event  
  23.         void btn_Hello_Click(object sender, EventArgs e)  
  24.         {  
  25.             //Display a Toast Message on Clicking the btn_Hello  
  26.             Toast.MakeText(this,"Hello World App by Anoop",ToastLength.Short).Show();  
  27.         }  
  28.     }  
  29. }  
Build and run the application. 
Final Preview 
 
I hope you like it. Thanks.  

Up Next
    Ebook Download
    View all
    Learn
    View all