Friday, 3 November 2017

Xamarin Forms Fast Splash Screen[Tutorial 29]

Step 1 : At First Create a new Activity
namespace SplashScreen.Droid
{
[Activity(Theme ="@style/Theme.Splash",
MainLauncher = true,
NoHistory =true,
Icon ="@drawable/clap")]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
System.Threading.Thread.Sleep(500);
StartActivity(typeof(MainActivity));
}
}
}
view raw SplashActivity hosted with ❤ by GitHub
Step2: Disable the Main Launcher Screen From Main Activity
[Activity(Label = "SplashScreen", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
view raw MainActivity hosted with ❤ by GitHub
step3:You Have To Add Styles on styles.xml
<style name="Theme.Splash"
parent="android:Theme">
<item name="android:windowBackground">@drawable/clap</item>
<item name="andorid:windowNoTitle">true</item>
</style>
view raw styles.xml hosted with ❤ by GitHub
Step3:Please Make Sure If you had The Image Icon Same as Selected inside Your Drawable Folder
Step4:Holaa Our Steps Are Completed
Hey Guys if you are using Older Verson of Xamarin.Forms in which MainActivity is Inheriting from Forms Application Activity then you should do this in your styles:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="Theme.Splash"
parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/icon</item>
</style>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#2196F3</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#1976D2</item>
<!-- colorAccent is used as the default value for colorControlActivated
which is used to tint widgets -->
<item name="colorAccent">#FF4081</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight and colorSwitchThumbNormal. -->
<item name="windowActionModeOverlay">true</item>
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
</style>
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="colorAccent">#FF4081</item>
</style>
</resources>
view raw styles.xml hosted with ❤ by GitHub
Similarly Your Splash Activity will took like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace XFormsContosoCookbook.Droid
{
[Activity(Theme = "@style/Theme.Splash",
MainLauncher = true,
NoHistory = true,
Icon = "@drawable/icon")]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
System.Threading.Thread.Sleep(500);
StartActivity(typeof(MainActivity));
}
}
}
Your AndroidManifest will LookLike this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.xamarin.contosocookbook" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="Xamarin Zero to Hero">
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>

No comments:

Post a Comment