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:
- <?xml version="1.0" encoding="utf-8" ?>
- <resources>
- <style name="Theme.Splash" parent="android:Theme">
- <item name="android:windowBackground">@drawable/splash</item>
- <item name="android:windowNoTitle">true</item>
- </style>
- </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:
- using Android.App;
- using Android.OS;
- using System.Threading;
-
- namespace Splash_Screen
- {
-
-
-
- [Activity(Label="Splash Screen App",MainLauncher=true,Theme="@style/Theme.Splash",NoHistory=true,Icon="@drawable/icon")]
- public class SplashScreen : Activity
- {
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
- Thread.Sleep(4000);
-
- StartActivity(typeof(Activity1));
- }
- }
- }
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:
- using System;
- using Android.App;
- using Android.OS;
- using Android.Widget;
-
- namespace Splash_Screen
- {
- [Activity(Label = "Hello World App")]
- public class Activity1 : Activity
- {
- Button btn_Hello;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
-
- btn_Hello = FindViewById<Button>(Resource.Id.btn_Hello);
-
- btn_Hello.Click += btn_Hello_Click;
- }
-
- void btn_Hello_Click(object sender, EventArgs e)
- {
-
- Toast.MakeText(this,"Hello World App by Anoop",ToastLength.Short).Show();
- }
- }
- }
Build and run the application.
Final Preview
I hope you like it. Thanks.