Learn to Code via Tutorials on Repl.it!

← Back to all posts
Create A Splash Screen In Xamarin.Android
devthepenguin

What is a Splash Screen?

The first screen you see for most of the apps on launching them for the first time that displays branding information or an identity that transitions into a different screen usually after a short span of time is known as the Splash Screen.

What is Xamarin.Android?

Xamarin.Android is part of Microsoft technology to develop Android apps based on .Net framework and C# as the primary development language. Xamarin.Android gives you the full power of the Android SDK to build apps for any Android platform. With C#, you can develop for phone, tablet, watch, and television.

App-start internals and Latency

Cold Start

When you launch an Android app on your smartphone supporting Android OS, the response time will be longer as the first time the instance is created or being deployed. Response time is determined by the mobile architecture comprising of both software and hardware components.

Hot or Warm Start

The application is kept alive for the next few seconds to anticipate any further potential calls, the system needs to bring the running app from the background to the foreground.

Setup and Installation

Microsoft Visual Studio is the platform for .NET development as well as it is specifically designed for Xamarin Development with all the right tools and features. You can download Visual Studio based on your development configurations from

https://visualstudio.microsoft.com/

Implement Splash Screen for your Android App

Create a new Xamarin.Forms project in Visual Studio and select blank template. Name your project as “SplashScreen” or any other name you like. Instead of creating a Xamarin.Forms project, you can simply create a Xamarin.Android project and follow the same steps. Xamarin.Forms projects can be chosen if you are interested in implementing the splash screen for iOS and UWP as well.

NuGet package manager

https://www.nuget.org/

NuGet is the package manager for .NET. The packages required for this project can be imported by NuGet package manager or Nuget package manager console. A NuGet package is a single .ZIP file with the extension .nupkg which contains compiled code (DLLs) required for specific tasks. For the app the following two NuGet packages are required to be installed which can be achieved by running the following command on the Package Manager console from tools to install the most stable version of android support library compatible with Xamarin.Android-

Define a background color for splash screen by adding the following value to the colors.xml file located in SplashScreen.Android > Resources > values > colors.xml

<color name="splash_background">#FFFFFF</color>

Next, right click on the drawables folder and select add an existing item to import an image from your device to display on the splash screen.
Create a new drawable item by adding a new file named splash_screen with .xml extension and add a LayerList item to it.

The background is set up and is ready for use.
Create a custom splash theme “ThemeSplash” by adding the following code to styles.xml file located in SplashScreen.Android > Resources > values > styles.xml and insert the background drawable created in the previous steps. This theme will be set for the SplashActivity class, defining the behaviour of events on being launched.

Create a new Activity named “SplashActivity” by adding a new item to SplashScreen.Android and add the following code. The following directives can be added manually if they are not present after adding the code for SplashActivity.cs in the gist below.

Understanding the code-behind

.Net Task based model and Async and Await keywords
Tasks are constructs used to implement what is known as the Promise Model of Concurrency. In short, they offer you a “promise” that work will be completed at a later point, for the current context the splash screen is launched and it is up and running for a given period of milliseconds.

await Task.Delay(8000);

On completion of this time period, the task has finished it’s execution. By default, tasks execute on the current thread and can be determined by adding the following line of code.

Log.Debug(TAG, Thread.CurrentThread.ManagedThreadID);

Final Steps

Launch your app
Build solution and launch the app on an appropriate device which can be a physical one or an emulator downloaded and installed on the Android Device Manager. Finally the result should be an image displayed on the screen, the Splash Screen is up and running.

Voters
DynamicSquid
devthepenguin
AlyaMubarak
techgeek680
Codemonkey51
Comments
hotnewtop
Codemonkey51

Cool!

devthepenguin
CodeLongAndPros

Nice but:

Microsoft .NET

.NET is bad.

DynamicSquid
CodeLongAndPros

@DynamicSquid It's literally Microsoft patented code.

DynamicSquid

@CodeLongAndPros oh yeah true, but why is that bad?

CodeLongAndPros

@DynamicSquid You can't use .NET on a non-Microsoft approved platform. You have to have VS and Windows for a 'best usage' I don't know if mcs can do NET

devthepenguin

@CodeLongAndPros well as far as dev platform compatibility is concerned, .NET is fairly well supported for Linux and macOS as it is for windows with an equally large user base and still MSBuild allows to build products in envs where VS isn't supported. Moreover it offers integration of services like ML.NET and .NET Core IOT which are free, open source and cross-platform, which really makes it versatile for shared code-base, and native development.