Sunday, 7 December 2014

Creating Splash Screen for Android App in Xamarin

Splash screen is an image that appears while an application is loading. Splash Screens are typically used to notify the user that program is in the process of loading. In this Article, We will learn How to create Splash Screen for Android Application in Xamarin / Mono for Android. In my previous Article, We saw How to create Hello World Application in Xamarin. Before starting, I suggest you to read my previous Article.


Let's Begin:
1) Add two images(icon.png for icon and splash.png for displaying image on Splash Screen) in Resources\Drawable folder.
Splash Screen in Xamarin image 1
2) Right Click on Values folder present in Resources folder of project -> Add -> New item.
Splash Screen in Xamarin Image 2
3) Select XML File and give it a Name Styles.xml. In Android Application, We can assign themes to an Activity.
Splash Screen in Xamarin Image 3

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 above Code, We have created a Theme named as Theme.Splash and Set the Window Background as splash.png image present in Drawable folder. We have also disabled title bar by Setting windowNoTitle to true.
4) Add new Activity. Right Click on Project -> Go to Add -> New Item.
Splash Screen in Xamarin Image 3
Select Activity and Give it a name SplashScreen.cs
Splash Screen in Xamarin Image 4

SplashScreen.cs Code:
using Android.App;
using Android.OS;
using System.Threading;

namespace Splash_Screen
{
    //Set MainLauncher = true makes this Activity Shown First on Running this Application
    //Theme property set the Custom Theme for this Activity
    //No History= true removes the Activity from BackStack when user navigates away from the Activity
    [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);
            //Display Splash Screen for 4 Sec
            Thread.Sleep(4000);
            //Start Activity1 Activity
            StartActivity(typeof(Activity1));
        }
    }
}
In Activity1.cs, We have added a Button with Id="btn_Hello" from ToolBox and Added Click Event and Display Toast message on Clicking the Button.
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
            SetContentView(Resource.Layout.Main);
            //Get btn_Hello Button control from the Manin.axml Layout.
            btn_Hello = FindViewById<Button>(Resource.Id.btn_Hello);
            //Creating Click event of btn_Hello
            btn_Hello.Click += btn_Hello_Click;
        }
        //btn_Hello Click Event
        void btn_Hello_Click(object sender, EventArgs e)
        {
            //Display a Toast Message on Clicking the btn_Hello
            Toast.MakeText(this,"Hello World App by Anoop",ToastLength.Short).Show();
        }
    }
}
Build and Run the Application.
Final Preview:
Splash Screen in Xamarin / Mono for Android
I hope you like it. Thanks.
[Download Source Code via Google Drive]
Watch Video:

6 comments:

  1. Thanks Mr Anoop Kumar Sharma

    ReplyDelete
  2. Hi Please Help me I'm Newbie on this Dev. I follow the whole instructions but I got this error. Severity Code Description Project File Line Suppression State
    Error CS0103 The name 'Resouce' does not exist in the current context Splash Screen Demo X:\Visual Studio\Projects\Splash Screen Demo\Splash Screen Demo\Activity1.cs 22 Active

    ReplyDelete
    Replies
    1. Hi Harvy, I think you typed Resource incorrectly. Look at the error carefully:

      The name 'Resouce' does not exist in the current context Splash Screen Demo

      Delete
  3. Hi, Please help me. I follow the whole instructions but I got this error.
    Unhandled Exception:



    System.TypeLoadException: Could not load type 'App_Android.SplashActivity, App_Android, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' from assembly ''.

    How can I solve it?

    ReplyDelete
  4. when i run the app its shows the error
    unsupported major.minor version 52.0
    i have visual studio 2015

    ReplyDelete

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Subscribe us on YouTube

Like us on Facebook

Follow us on Google+