0
votes

I am creating my first Xamarin Forms cross-platform application. I am in the process of creating a splash screen using the native platform code for each OS.

My Android splash screen is not displaying. I trace through the application and it hits the correct code, but the page does not display.

My xml file is in the resources\layout folder which renders nicely when I view it. It is called SplashScreen.xml.

Can anyone shed some light as to why the page does not display?

{
    [Activity(MainLauncher = true, NoHistory = true)]
    public class SplashScreenActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            RequestWindowFeature(WindowFeatures.NoTitle);

            base.OnCreate(savedInstanceState);

            SetContentView(Droid.Resource.Layout.SplashScreen);

            System.Threading.Thread.Sleep(3000);
            StartActivity(typeof(MainActivity));


        }

        public override void OnBackPressed() { }
     }
}


2
Can you also post your xml file?javachipper

2 Answers

0
votes

Better Approach

  1. In your Resources -> Values -> styles.xml create your splash screen theme.

    <?xml version="1.0" encoding="utf-8" ?>
     <resources>
      <style name="splashscreen" parent="Theme.AppCompat.Light.NoActionBar">
       <item name="android:windowBackground">@drawable/splashscreen</item>
       <item name="android:windowNoTitle">true</item>
       <item name="android:windowIsTranslucent">false</item>
       <item name="android:windowIsFloating">false</item>
       <item name="android:backgroundDimEnabled">true</item>
      </style>
    
  2. Go to your MainActivity.cs and ensure MainLauncher = true. Ensure no other activity has MainLauncher=true.

  3. In MainActivity.cs change the default theme to the splash screen.

    [Activity(Label = "Mobile App", Theme = "@style/splashscreen", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTop)]
    public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    
  4. In OnCreate, switch your theme with the below code.

    protected override void OnCreate(Bundle bundle)
    {
        base.Window.RequestFeature(WindowFeatures.ActionBar);
        // Name of the MainActivity theme you had there before.
        // Or you can use global::Android.Resource.Style.ThemeHoloLight
        base.SetTheme(Resource.Style.MainTheme);
    
        base.OnCreate(bundle);
    
        ...
    

Please refer this blog.

0
votes

You should write a theme to load your xml first, like this:

<resources>
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light">
  </style>

  <style name="MyTheme" parent="MyTheme.Base">
  </style>

  <style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/SplashScreen</item>
    <item name="android:windowNoTitle">true</item>  
    <item name="android:windowFullscreen">true</item>  
    <item name="android:windowContentOverlay">@null</item>  
    <item name="android:windowActionBar">true</item>  
  </style>
</resources>

Then use that theme in the activity:

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]

Here is the document about Splash Screen in Xamarin.Android and there are detailed steps you can follow.

A sample project is also available : splashscreen.