Xamarin.Forms Splashscreen in Android

2»

Posts

  • Actually, it's not a bug, on Xamarin Forms you can't have more than one classe inheriting from FormsApplicationActivity, I change the splash screen base classe to Activity and now, everything is working fine! Thanks to Xamarin support

  • DeanFaizalDeanFaizal USUniversity

    Thanks @Alexandrem‌ !

    Mine was crashing because I had the Splash activity inheriting from "AndroidActivity" instead of just "Activity"

  • DemiVisionDemiVision USMember ✭✭

    @nbevans I tried your solution and it is definitely much cleaner than the separate SplashActivity. However, the latest versions of Xamarin.Forms use a LoadApplication( new App() ); call in the MainActivity template. This call crashes with a NullReference when using your solution. If I remove the theme, it works fine. Any ideas? Thanks in advance!

  • nbevansnbevans USMember ✭✭✭

    @TikiMotel I tested our app last week on 1.3.x and the splash was still working fine? As it happens we had an issue elsewhere and had to rollback for the time being.

    I only made the minimalist of changes concerning LoadApplication etc but functionally nothing much had changed from the original example code I pasted on this thread.

  • DemiVisionDemiVision USMember ✭✭

    @nbevans thanks for the response. I've found a couple other posts with the same issue I'm facing, with no resolution. I'm not sure what I'm doing differently. The new LoadApplication() call and inheriting from FormsApplicationActivity are the only differences I can find. Out of curiosity, what's your min Android version?

  • nbevansnbevans USMember ✭✭✭

    API 17... target 21.

    I think the issue you have is something else, unrelated to the Zygote-based splash screen.

  • DemiVisionDemiVision USMember ✭✭

    Unfortunately, it 100% comes and goes with addition of the Theme to the MainActivity. As soon as I add Theme= @style/Theme.Foo, then LoadApplication() crashes. Remove the Theme, works fine. It also works fine if I have the theme on a separate SplashActivity but not on the MainActivity. Bizarre.

  • Does anybody have this problem: Open the app, press back button (the app closes), Open the app and navigate to any place: The app crashes!

  • Mike.9461Mike.9461 USMember

    DanielL, I am new to Xamarin Forms, could you update a sample code, how to do splash screen for iOS and Andorid in PCL coding, only put the images in respective resources.

    Alternatively you can email also : [email protected]

    Thanks

  • ShamnadShamnad USMember ✭✭
    edited June 2015

    hello friends , me to have problem in splash screen according to me my app have a theme when i add the theme for splash, it makes error while the theme removed it works but no images displaying on it , now splash screen is displaying but no image in the activity (shall we add one theme only for the splash and other for the remaining app )

  • ShamnadShamnad USMember ✭✭

    update for my comment :smiley: my error solved it works fine now

  • DanielLDanielL PLInsider ✭✭✭✭

    @Mike.9461 There's no difference when using PCL project

  • JohnHardmanJohnHardman GBUniversity mod

    I'm not (currently) an Android person, so forgive me if this is a newbie question.

    When a splash screen is displayed, if the user changes the orientation, what needs to be done to prevent the splash screen's aspect ratio being changed to stretch the same image to fit the new width/height? Are two images supplied, or is there some way of saying to maintain the aspect ratio and pad the rest with a particular color (or similar)?

    Thanks,

    John H.

  • FredyWengerFredyWenger CHInsider ✭✭✭✭✭

    @JohnHardman:
    I have tried to implement an Android splash screen that works as expected for all situations... and gave up after days... (there are simply very (too) many cryptic android options).
    I have found a solution now with only one Image that is scaled mostly correct (in some case the aspect ratio is not kept correct 100% but maybe 80%).
    The quality is not optimal on any device, but for me it's O.K. (I don't want to spend further days to find out, how to implement it for any case, any device and any device-resolution correct and add a lot of further images to the project).
    But.. I'm also not an "Android person" :smirk:
    Let's see, if you receive a good (better) answer here...
    If not (and the splash screen don't have to be perfect for you :wink:), feel free to ask me again...

  • FrancoisMFrancoisM FRUniversity ✭✭

    Hi,

    I followed what's been said in this post. My app compiles and run fine (XF 2.0) but no splascreen just a black screen.
    The Splascreen.xml file is taken into account as if I change the bitmap src to a non existing file the project doesn't compile.
    Here is my code.

    MainAtivty.cs

        [Activity(Label = XamarinCommon.Settings.ActivityName, Icon = "@drawable/icon", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.KeyboardHidden, ScreenOrientation = ScreenOrientation.Portrait, Theme = "@style/Theme.Splash")]
        [IntentFilter(new string[] { XamarinCommon.Settings.PackageName + ".MESSAGE" }, Categories = new string[] { "android.intent.category.DEFAULT" })]
        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
        {
            protected override void OnCreate(Android.OS.Bundle bundle)
            {
                base.SetTheme(global::Android.Resource.Style.ThemeHoloLight);
                base.OnCreate(bundle);
    
                if (bundle != null)
                    Console.WriteLine("App restarted");
    
                global::Xamarin.Forms.Forms.Init(this, bundle);
    
                LoadApplication(new App(ConvertPixelsToDp(Metrics.WidthPixels), ConvertPixelsToDp(Metrics.HeightPixels)));
            }
    

    Styles.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <resources>
      <style name="Theme.Splash"
        parent="android:Theme">
        <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>
    </resources>
    

    SplashScreen.xml

        <?xml version="1.0" encoding="utf-8" ?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
          <item>
            <shape android:shape="rectangle" >
              <solid android:color="#FFFFFF" />
            </shape>
          </item>
          <item>
            <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
              android:src="@drawable/icon"
              android:gravity="center"
              android:layout_gravity="center"/>
          </item>
        </layer-list>
    
  • CTPAXCTPAX USMember

    Hi guys!

    Proposed solution works great. Unfortunately my app takes a while to initialize (about 2-3 secs) at the MainActivity.OnCreate method. So in the scenario above I can see nice splash screen, then it disappears and I see my ugly while background of MainActivity and navigation bar for 2-3 seconds (which shouldn't be there at all). I applied same style for main activity to change background and hide actionbar but in this case Xamarin.Forms throws an exception:

    System.NullReferenceException: Object reference not set to an instance of an object
      at Xamarin.Forms.Platform.Android.Platform.UpdateActionBarBackgroundColor () [0x00010] in <filename unknown>:0
      at Xamarin.Forms.Platform.Android.Platform.set_CurrentNavigationPage (Xamarin.Forms.NavigationPage value) [0x000f1] in <filename unknown>:0
      at Xamarin.Forms.Platform.Android.Platform.UpdateActionBar () [0x00063] in <filename unknown>:0
      at Xamarin.Forms.Platform.Android.Platform.SetPage (Xamarin.Forms.Page newRoot) [0x000bd] in <filename unknown>:0
      at Xamarin.Forms.Platform.Android.FormsApplicationActivity.InternalSetPage (Xamarin.Forms.Page page) [0x000b7] in <filename unknown>:0
      at Xamarin.Forms.Platform.Android.FormsApplicationActivity.SetMainPage () [0x0000c] in <filename unknown>:0
      at Xamarin.Forms.Platform.Android.FormsApplicationActivity.LoadApplication (Xamarin.Forms.Application application) [0x0002d] in <filename unknown>:0
      at Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x0003a] in 
    

    Can I initialize Xamarin.Forms app at SplashScreen activity and then when it's ready navigate to MainActivity?

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    @nbevans Thanks a lot! It work at the first try. Is there a way how to make the splash screen full screen?

  • CN_AndersonCN_Anderson USUniversity ✭✭

    @nbevans Great solution. Two questions for you (or anyone else following this post):

    1. What does the code base.Window.RequestFeature(WindowFeatures.ActionBar); do? I ran my app with and without it and I can't see the difference. I did find this android reference, that says that it, "Enable[s] extended screen features", but I am not sure what that means.

    2. I found a post on Stack Overflow that referenced this site that says splash screens can lead to memory leaks because of the strong association with the parent, preventing garbage collection. Have you seen any evidence of that with this approach?

  • This does work fine to show the splash screen, but is there a way to stop it from registering input?
    When the splash screen is shown and you touch the screen, as soon as the main activity shows this input is executed. For example if I tap the splash screen at the position where my first activity has a button, this button is pressed as soon as the activity shows.
    IMO the splash screen shouldn't capture any touch input at all.

  • JohnHardmanJohnHardman GBUniversity mod

    Has anybody solved the problem of the splash screen aspect ratio not being maintained if the user changes the orientation on Android? At the moment, my splash screen is getting stretched horizontally if the user changes the Android device to have landscape orientation.

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    @JohnHardman have you tried using a 9-patch image?

  • NMackayNMackay GBInsider, University mod
    edited April 2016

    @DavidDancy said:
    @JohnHardman have you tried using a 9-patch image?

    I'd be curious as the same issue existed with 9-patch images in Icenium (Appbuilder).

  • BishwajitNepaliBishwajitNepali USMember ✭✭

    This is the perfect solution thank you so much Daniel Luberda

  • How do I use base.SetTheme(); to set a theme I have defined in my styles.xml, so just MainTheme ?

  • I did my splash screen based on your examples and it worked here are my files.
    I am using "Blank App (Xamarin Forms Portable"

    MainActivity.cs

    namespace Splash.Droid
    {
    //[Activity(Label = "Splash", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    [Activity(Label = "Splash", Theme = "@style/MainTheme",
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
    protected override void OnCreate(Bundle bundle)
    {
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);
    
            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }
    }
    

    }

    SplashScreen.cs

    namespace Splash.Droid
    {
    [Activity(Label = "Splash", MainLauncher = true, NoHistory = true, Theme = "@style/Theme.Splash",
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class SplashScreen : Activity
    {
    protected override void OnCreate(Bundle bundle)
    {
    base.OnCreate(bundle);

            var intent = new Intent(this, typeof(MainActivity));
            StartActivity(intent);
            Finish();
        }
    }
    

    }

    styles.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <resources>
    
      <style name="MainTheme" parent="MainTheme.Base">
      </style>
      <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">#2196F3</item>
        <item name="colorPrimaryDark">#1976D2</item>
        <item name="colorAccent">#FF4081</item>
        <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>
      <style name="Theme.Splash" parent="android:Theme">
        <item name="android:windowBackground">@drawable/splash</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>
    </resources>
    

    SplashScreen.xml

        <?xml version="1.0" encoding="utf-8" ?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
      android:src="@drawable/icon"
      android:gravity="center"
      android:layout_gravity="center"/>
    
  • CozziWazziCozziWazzi USMember

    hmm im getting 2 launch icons like there is 2 seperate projects installed

  • SuatKorkmazSuatKorkmaz USMember ✭✭✭

    @CozziWazzi said:
    hmm im getting 2 launch icons like there is 2 seperate projects installed

    Set MainLauncher = false on your MainActivity

  • Felix.xFelix.x USMember ✭✭

    I found a better solution (at least it claims to be better) at https://xamarinhelp.com/creating-splash-screen-xamarin-forms/ . It uses a theme switch technique which I found indeed better in old android phones because it avoids the activity switch flash.

  • SreeeeSreeee INMember ✭✭✭✭✭

    @CTPAX Use jdk 8 instead of jdk 9. It will solve NullReferenceException

  • FaizalSaidaliFaizalSaidali USMember ✭✭
    edited February 2018

    Hi Xamarin,

    I have stucked on a issue while adding Splash screen. I have implement the splash screen with help of this link.

    After that the app crashed. and show the given error popup.

    Please help me
    Thank you

  • curtis.ehrhartcurtis.ehrhart USMember ✭✭
    edited May 2018

    I couldn't get these examples to work, but I was able to put something together..

    MainActivity.cs

    using Android.App;
    using Android.Content.PM;
    using Android.OS;
    using Xfx;
    
    namespace Portable.Droid
    {
    
        [Activity(Label = "myApp", MainLauncher = false, NoHistory = true, Theme = "@style/myStyle",
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
        public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.SetTheme(global::Android.Resource.Style.ThemeHoloLight);
    
                //base.SetTheme(Resource.Style.acsStyle);
                base.OnCreate(bundle);
    
                //this is for floating labels on Entry fields
                XfxControls.Init();
    
                global::Xamarin.Forms.Forms.Init(this, bundle);
                Xamarin.FormsMaps.Init(this, bundle);
                LoadApplication(new App());
            }
        }
    

    SplashScreen.cs

    using Portable.Droid;
    using Android.App;
    using Android.Content;
    using Android.Content.PM;
    using Android.OS;
    
    [Activity(Label = "Splash", MainLauncher = true, NoHistory = true, Theme = "@style/SplashTheme",
     ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
     public class SplashScreen : Android.Support.V7.App.AppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            var intent = new Intent(this, typeof(MainActivity));
            StartActivity(intent);
            Finish();
        }
    }
    

    Resources\Values\Styles.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <resources> 
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
    </style>
    </resources>
    

    Resourse\drawable\background_splash.axml

    <?xml version="1.0" encoding="utf-8" ?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item>
        <shape android:shape="rectangle" >
          <solid android:color="#FFFFFF" />
        </shape>
      </item>
      <item>
        <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
          android:src="@drawable/logo_large"
          android:gravity="center"
          android:layout_gravity="center"/>
      </item>
    </layer-list>
    
Sign In or Register to comment.