locked
Xamarin.Forms Splashscreen in Android RRS feed

  • Question

  • User54080 posted

    Hi, In Forms how to add SplashScreen to Android appliaction? Thanks in advance.

    Friday, June 27, 2014 12:52 PM

All replies

  • User2773 posted

    I do it that way:

    SplashScreen.cs:

    [Activity(Label = "MyApp", 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();
        }
    }
    

    MainActivity.cs:

    [Activity(Label = "MyApp", Theme="@android:style/Theme.Holo", 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : AndroidActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            Xamarin.Forms.Forms.Init(this, bundle);
            SetPage(App.GetMainPage());
        }
    }
    

    Resources.Drawable.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"/>
    

    Resources.Values.Styles.xml:

    <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>
    
    Friday, June 27, 2014 1:04 PM
  • User112 posted

    Out of curiosity, why are you calling Finish in SplashActivity?

    Wednesday, July 23, 2014 9:31 AM
  • User2773 posted

    @MihaMarkic Good catch, with NoHistory = true attribute set on SplashActivity, there's no need to call Finish, activity is automatically finished when another one is opened.

    Wednesday, July 23, 2014 11:00 AM
  • User54080 posted

    Thank you DanielL, you solution is working.

    Friday, July 25, 2014 11:16 AM
  • User79313 posted

    Being new to Xamarin development and just finishing off my first Xamarin.Forms app I have just a small question :-)

    What screen resolutions do I need to support and where should they be setup?

    many thanks in advance on this one - this is my last nut to crack!

    Wednesday, October 15, 2014 9:03 PM
  • User79313 posted

    A couple of small things - 1. the SplashScreen.cs goes in the Forms portable or Android project? 2. the SplashScreen ctor gets called via the SetPage redirection to the portable project? 3. the graphic is an Image in the SplashScreen.xaml or a resource defined elsewhere (see first question)?

    Basically I am not sure how the sequence of event works here.

    Wednesday, October 15, 2014 9:20 PM
  • User2773 posted

    @GrahamMcDonald?

    1. SplashScreen.cs goes into an Android Project, as all other Android specific files.
    2. Yes, here: SetPage(App.GetMainPage()); - GetMainPage returns new instance of the SplashScreen Activity
    3. SplashScreen.xml is a definition of a bitmap (you can tune size, centering, etc) which includes custom drawable (@drawable/icon). You can also change it to any custom canvas or drawable (http://developer.android.com/guide/topics/graphics/2d-graphics.html)

    What screen resolutions do I need to support and where should they be setup?

    You should do exactly the same as any native Android App: http://developer.android.com/guide/practices/screens_support.html

    If there's no resource for a specific device size - it is scaled (up/ down).

    Thursday, October 16, 2014 11:25 AM
  • User65389 posted

    @DanielL:

    Hi Daniel
    First, thanks a lot that you have posted your implementation here…
    I am trying to implement a splash screen in the Android-project of my XF-App (according to your description) and have some problems (as I have no experience with Android).

    Note: I don’ have used a theme in my android-project until now.
    Therefore, I don’t have an entry Theme="@android:style/Theme.Holo", in my MainActivity.cs

    I have now:

    • Added an Image (to show at startup) named SplashScreen.png to my \Resources\-Folder in the Android-project
    • created a SplashScreen.cs file in the root of my XF-Android-project (Changed “MyApp” with the real name of my app ;-)
    • Leaved ,Theme = "@style/Theme.Splash” from your example
    • Created a new folder \Resources\Values\ (I think, you mean \Resources\Values\Styles.xml), if you write Resources.Values.Styles.xml)?
    • Created a new file “Styles.xml” in this folder (overtake your example in the Styles.xml file)

    If I try to compile, I have the error:
    No resource found that matches the given name (at 'android:windowBackground' with value ' @Drawable/SplashScreen
    I also have tried to change SplashScreen to SplashScreen.png - same error-message
    Not sure, if the image is not found, or ‘android:windowBackground' is not found...?

    If I try to add also the SplashScreen.xml (in the \Resources\Drawable-folder), I have and additional error-message “Resource entry splashscreen is already defined.” To the SplashScreen.xml-file.

    As I don’t see the context of the files (which of the files I really need), I'm also cannot see, where the problem lies.

    I specially also don’t see the link to the image in your example (or does the image in your example don’t have a file-extension / does the reference to the image have to be wrote without extension for a special reason?)

    Can you please bring some light in my darkness and tell me, what I have to do, to bring the SplashScreen to work…?

    Thanks very much for your help!

    Monday, November 24, 2014 2:49 PM
  • User65389 posted

    Update: I have changed the image-name and also the path to the image to lowercase (although my drawable-folder has uppercase (\Drawable):

    <item name="android:windowBackground">
          @drawable/splashscreen
    

    Now, I am able to compile and debug (without SplashScreen.xlm), but.. my app is deployed two times to the device now (one time with splashscreen, one time without)... very strange...!
    Also the image is stretched what looks terrible (I think (hope) that there are some settings, how to display the image (aspect-ration, etc.).

    Daniel: Do you have an idea, what can cause the "dual-deployment" (both icons are removed first by deploy and the copied once again)?

    Monday, November 24, 2014 3:22 PM
  • User2773 posted

    @FredyWenger? Yes, theme Theme.Splash should be in Resources->values->Styles.xml

    Resources.Drawable.SplashScreen.xml is very important. It is used in Theme.Splash definition. And prevents image from stretching!!! (centers it)

    Check if your resources file have Build Action set to AndroidResource it is also very important.

    but.. my app is deployed two times to the device now (one time with splashscreen, one time without)... very strange...!

    Check SplashScreen and MainMenu activity attributes. You probably have MainLauncher set to true on both of them.

    SplashScreen.cs:

    [Activity(Label = "MyApp", MainLauncher = true, NoHistory = true, Theme = "@style/Theme.Splash", 
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    

    MainActivity.cs:

    [Activity(Label = "MyApp", Theme="@android:style/Theme.Holo", 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    
    Monday, November 24, 2014 3:53 PM
  • User65389 posted

    @DanielL: Thanks for your (fast) reply .-)
    Both: my "real" splashscreen(.png) and splashscreen.xml are marked as AndroidResource.
    In both .cs-files (SplashScreen.cs and MainActivity.cs) the option MainLauncher = true is included.
    But in MainActivity.cs, I don't have included Theme="@android:style/Theme.Holo"
    Do I really need this (I think, this is for the whole app and I don't want a theme)?
    As soon as I include splashscreen.xml in the project, I have the error "Resource entry splashscreen is already defined."
    Note: The image splashscreen.png and also splashscreen.xml are stored in the resources-Folder (same name, but different extension).
    As I wrote in my posting, I don't see the real link in your example to the image...?
    Thanks for a further reply...

    Monday, November 24, 2014 4:29 PM
  • User2773 posted

    @FredyWenger?

    In both .cs-files (SplashScreen.cs and MainActivity.cs) the option MainLauncher = true is included.

    This is wrong. It should only be in SplashScreen.cs.

    As soon as I include splashscreen.xml in the project, I have the error "Resource entry splashscreen is already defined."

    As the error states, you have duplicate definitions! Try searching in drawable-hdpi, drawable-mdpi etc folders. It should only be in drawable" folder.

    Note: The image splashscreen.png and also splashscreen.xml are stored in the resources-Folder (same name, but different extension).

    Change the name of splashscreen.png to something else.

    Monday, November 24, 2014 4:36 PM
  • User65389 posted

    @DanielL:
    It seems, as the MainLauncher = true has caused the double install (I have removed it now from the MainActivity.cs and it seems, as this problem has gone:-) I don't have any duplicate definitions in any subfolder...
    But.. as I wrote... the splashscreen.xml and my (real) image splashscreen.png have the same name but another extension and I think, this causes the problem.
    But... I think I see now the (my) confusion:
    In your example, you have: android:src="@drawable/icon

    I think' that's the (my) "missing link" from SplashScreen.xml to the real image (in your project named Icon)

    So.. I have changed the name of my image from SplashScreen.png to AndroidSplashScreen.png, changed
    android:src="@drawable/icon to android:src="@drawable/AndroidSplashScreen.png and now...
    ...all seems to work (including not to stretch the image, if I start the app in landscape-orientation:-))

    So thank you very much, for your help!

    And.. for other newbies (like me;-):
    Resources.Drawable.SplashScreen.xml means \Resources\Drawable\SplashScreen.xml (for all files in example)
    If you don't have already a subfolder \Resources\Values (for Resources.Values.Styles.xml) you have to create it.
    You don't have to add the Theme="@android:style/Theme.Holo", in your MainActivity.cs (if you don't have it already / if you don't want it)
    In your files, always write drawable lowercase (also, if your folder in real world is named with uppercase (\Drawable)
    In your MainActivity.cs you have to remove MainLauncher = true as soon as you activate your SplashScreen.cs with MainLauncher = true (else your app will be "doubled" by deploying to the device).

    Finally once more: thanks a lot Daniel - you have helped me (and I think many other developers) al lot with your posting!

    Monday, November 24, 2014 5:17 PM
  • User2773 posted

    @FredyWenger? Not a problem! It's nice to hear it's working now ;)

    Monday, November 24, 2014 5:27 PM
  • User65389 posted

    @DanielL:
    I have a last question - maybe you can help "in short".
    If not, I will post the question in the Android-forum (so that you don't have to waste your time;-)

    As I wrote, the SplashScreen works now as expected - I only have a small issue.
    If I also can fix this small issue, the SplashScreen will work perfectly for me.
    Issue short-description:
    I know, that I should create various images for various device-sizes and device-resolutions, to have a "perfect look" on any device. But I don't have the time to create the images now and - I think - also the size of the final app then will increase much... so - at least for now - I want to include a single image to display on all devices.
    My main goal is, that a "viewable" Image is displayed by start of the app (instead of a "black screen".
    Therefore, I have taken my (already existing) splash-screen for iOS (IPhone) also for Android. This works fine (Android-Phone, Android-Tablet) with one small issue:
    - If I start the App on my tablet (Samsung SM-T900) in portrait-orientation:
    - The image is scaled (zoomed) with the correct aspect-ratio and showed centered (also the quality is O.K.)
    => Perfect!
    - If I start the App on my tablet (Samsung SM-T900) in landscape-orientation:
    - the images is showed in correct aspect-ratio centered in the screen (what is correct), but not scaled, so that the image is cut (not the whole image is showed)

    In addition to your example, I have added android:adjustViewBounds="true" (as this - according to the Android-description, I found here developer.android.com/reference/android/R.attr.html#adjustViewBounds, should "adjust the ImageView to its bounds to preserve the aspect ratio of its drawable"...
    Unfortunately, it seems, as this option don't change the behavior (maybe not together with the other "gravity-options"?).

    My whole SplashScreen.xml:

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

    Question:
    Don you have an idea, how I can reach my goal (center the image, size the image to the available space by keeping the aspect-ratio)..?

    If not... no problem - I will post the question in the Android-Forum...
    Thanks for a short replay...

    Wednesday, November 26, 2014 10:40 AM
  • User2773 posted

    @?FredyWenger If you want to provide only one drawables size it is recommended to put them into "drawable-xxhdpi" folder (480dpi). You could also experiment with adding "drawable-land-xxhdpi" which is only used in landscape modes.

    Wednesday, November 26, 2014 11:21 AM
  • User65389 posted

    @DanielL:
    Thanks for your reply.. but does this change the behavior of how the image is scaled generally (phone/tablet...portrait(landscape..?).
    As I wrote, the quality is O.K. for me (an for now), the only problem is, that the image isn't scaled down, if the resolution (height) is to small...
    I thought (hoped), that there should be another "property" in SplashScreen.xml (or some combination of properties)?

    Wednesday, November 26, 2014 11:47 AM
  • User2773 posted

    Yes, It should scale images down.

    XXHDPI to XHDPI and lower

    XHDPI to HDPI and lower

    and so on...

    Scaling factors are:

    ldpi = 0.00625 * 120 -> 0.75

    mdpi = 0.00625 * 160 -> 1.0

    hdpi = 0.00625 * 240 -> 1.5

    xhdpi = 0.00625 * 320 -> 2.0

    xxhdpi = 0.00625 * 480 -> 3.0

    xxxhdpi = 0.00625 * 640 -> 4.0

    If you use default "drawable" folder it is scaled as it was MDPI (almost, with some exceptions etc) and if your device is >MDPI (almost all devices now) it probably won't be scaled down

    Wednesday, November 26, 2014 12:25 PM
  • User65389 posted

    @DanielL:
    Thanks a lot... I will check it out... :-)

    Wednesday, November 26, 2014 1:09 PM
  • User65389 posted

    @Daniell:
    I now have:
    - created a high-density image
    - created a folder \drawable-xxhdpi\ (parallel to the existing folder \drawable\
    - changed (in SplashScreen.xml -> stored in \drawable):

    android:src="@drawable-xxhdpi/AndroidSplashScreen"

    Note: The file is named AndroidSplashScreen.png (it seems, as the extension doesn't matter here) .
    The problem is, that the .png isn't found....
    Error-message:

    No resource found that matches the given name (at 'src' with value '@drawable-xhdpi/AndroidSplashScreen')

    The image is marked as AndroidResource... The spelling is correct.... What can this be...?

    Thanks a lot...

    Thursday, December 4, 2014 5:56 PM
  • User65389 posted

    Update to my last post:
    If I leave the src= to @drawable (as it was before) and only additionally include the new folder with the new .png:
    - the deploy to my tablet works:
    - the new image is used automatically
    - but the image is not scaled (cut on all sides)
    - the deploy to my phone (ZWX C2) crashes (reboot of phone)

    Thursday, December 4, 2014 6:16 PM
  • User2773 posted

    You're doing it wrong, don't do anything like android:src="@drawable-xxhdpi/AndroidSplashScreen" Android automatically chooses folder to suit your device, don't add any xxhdpi etc

    Friday, December 5, 2014 1:13 AM
  • User65389 posted

    @Daniell:
    I have seen that (see "Update to my last post") .
    As I wrote, with the new \drawable-xxhdpi-folder and the hi-res image:
    - I can deploy to my tablet, the new image is taken, but not downscaled
    => Your wrote in your earlier posting, that the image should be downscaled
    => Do I missing something?

    - I can deploy to the ZWX C2 Android-Phone
    => If the app then is started (by debugger or from phone), the app crashes hard and reboot the device
    => I think, it is not able to show the image what causes the crash
    => I wonder, that this happens as the old (smaller) version of the image further is stored under \drawable\ and
    android should take the smaller image automatically...?
    ==> Do you have an idea what can cause the hard-crash (with reboot)...?

    Friday, December 5, 2014 8:38 AM
  • User2773 posted

    Hello @FredyWenger?,

    Please read this:

    http://developer.android.com/guide/practices/screens_support.html

    http://developer.android.com/design/style/devices-displays.html

    In Xamarin.Android (and also Xamarin.Forms for Android), it's exactly the same. There is a lot of useful informations there.

    Sunday, December 7, 2014 4:25 PM
  • User21224 posted

    Splash screens aren't good practice. Any time you have a SplashScreenActivity, especially with some sort Thread.Sleep happening, you should think twice.

    The proper way to do splash screen on Android is to provide a 9-patch image that can be displayed by the app's Zygote process during startup. Read about the Zygote process on Android to know more.

    Monday, December 8, 2014 4:14 AM
  • User2773 posted

    @nbevans? Isn't it what we are using here? (With a difference that we're using bitmap instead of 9patch and using another activity for splash, which we can change to only using MainActivity with SetTheme method in OnCreate()). Can you post some details?

    When an activity is launched, Android starts a Zygote, an empty activity which does nothing, and sets your activity theme on it, then launches it. Once your activity is ready for display, it swaps the displayed activity to yours. For more info about Zygote, you can read this article by Cyril Motier

    So to answer your question, you can do this :

    Create a small theme which a custom window background displaying your splash info (you can use 9-patch to center unscaled content); In your manifest, use this splash theme for your activity; in your activity's onCreate() method, call setTheme(R.id.yourActivityTheme) (call it before setContentView() ); enjoy... That way, your "splash screen" (ie : the zygote with your splash theme) will be visible until your activity is ready to be displayed.

    Monday, December 8, 2014 8:29 AM
  • User21224 posted

    It's better to use your MainActivity and then swap the theme back in OnCreate to Holo or whatever you're using.

    Here's how I do it.

    [Activity(
        Label = "@string/ApplicationName",
        Icon = "@drawable/app_icon",
        MainLauncher = true,
        Theme = "@style/Theme.InterstitialZygote",
        LaunchMode = LaunchMode.SingleInstance,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
        WindowSoftInputMode = SoftInput.AdjustPan)]
    
    public class MainActivity : AndroidActivity {
        ....
        protected override void OnCreate(Bundle bundle) {
            // Revert theme and window features from the interstitial zygote.
            base.Window.RequestFeature(WindowFeatures.ActionBar);
            base.SetTheme(global::Android.Resource.Style.ThemeHoloLight);
            base.OnCreate(bundle);
            base.ActionBar.Hide();
    
            // Initialize Xamarin Forms engines.
            Forms.Init(this, bundle);
            FormsMaps.Init(this, bundle);
    
            // Initialize sync accounts and frequencies.
            SyncSetup.Init(this, TimeSpan.FromMinutes(60), TimeSpan.FromHours(3));
    
            // Bootstrap the application.
            app = App.Start(SetPage, Finish);
    
            // Show the action bar.
            base.ActionBar.Show();
    
            // Process handling of the intent (if any) that was used to start this activity.
            OnNewIntent(Intent);
        }
        ....
    

    }

    Here's my theme style XML:

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
        <style name="Theme.InterstitialZygote" parent="android:Theme.Holo.Light">
          <item name="android:windowActionBar">false</item>
          <item name="android:windowTitleSize">0dp</item>
          <item name="android:windowBackground">@drawable/interstitial_zygote</item>
        </style>
      </resources>
    

    interstitial_zygote is my 9-patch image in a variety of resolutions. 9-patches let you center a discrete full-alpha PNG image without needing any additional styling like layered drawables. The zygote/splash isn't meant to be complex, it's used in a variety of situations in Android including during an OnResume of your activity in some cases. Therefore keep it very simple, discrete and minimalist is the best convention. Definitely no animations or "loud" branding.

    Monday, December 8, 2014 8:45 AM
  • User2773 posted

    @nbevans? So the difference from my earlier example is not using separate activity for splash but changing theme in OnCreate method? Nice approach. It can be less resource consuming to avoid using separate activity for a splash.

    Monday, December 8, 2014 9:17 AM
  • User65389 posted

    @DanielL:
    Thanks for your input. I have read the information's behind your links before and unfortunately don't found any clear information's.
    What I want (try to reach), is to include a (one) Image in my app, that is displayed as splash-screen and is scaled at runtime.
    "Scaled" = centered on screen, downscaled (= whole image is visible, nothing cut) with the correct aspect ratio.
    According to your postings, I hoped, exactly that should be possible with a (one) xxhdpi-image.
    As I wrote: - on tablet-device, the xxhdpi-image is showed with correct aspect-ration but not scaled (=no solution for me)
    - on phone-device, the phone (not only the app) crashes, if i include the xxhdpi-image in project (= now solution for me)
    => It seems, as it (unfortunately) is not possible, to scale the image correct in Android.- :-(
    I hoped, that there would be some special settings (that I don't have found until now), that are doing the job...
    @nbevans?: Thanks also for your reply.
    If I understand your post correct, you say, that the splashscreen (with this implementation) also can be displayed (unwanted) by other events (Thread.Sleep).
    Do I understand you correct?

    Create a small theme which a custom window background displaying your splash info (you can use 9-patch to center unscaled content);

    "Unscaled content" means, that a large (e.g. xxhdpi)-image is not scaled (with correct aspect ratio) to fit the available page-space?

    Monday, December 8, 2014 9:26 AM
  • User21224 posted

    @DanielL yeah and it also saves a bunch of work with "forwarding" new intents onto the MainActivity.

    Monday, December 8, 2014 9:42 AM
  • User2773 posted

    @nbevans? I'll definitely try it!

    @FredyWenger? It's possible. On solution is to use only xxhdpi drawable, then:

    SplashScreen.xml

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

    Create few versions of SplashScreen.xml (in hdpi, xhdpi, xxhdpi, etc folders) and modifying them to use ScaleDrawable (http://developer.android.com/reference/android/graphics/drawable/ScaleDrawable.html) or something similar with appropriate scaling set for each of device screen sizes.

    SplashScreen.xml is responsible for how you'll render your bitmap.

    BTW: In my opinion it is better to create different sized bitmaps for each device size (performance!)

    Monday, December 8, 2014 10:37 AM
  • User65389 posted

    @DanielL (and @ all others that want only one splash-screen for Android)

    I heave searched now the Internet for hours and have tried all suggested versions of parameter-combinations... nothing have worked...
    It's unbelievable, how many options (and combinations!) exits for Android and how bad basic needs (use one image, scale it in the correct apspect ratio in the available space of the device) are described...
    I was minutes away to give up...
    Now, I have found a configuration, that works for me:

    <?xml version="1.0" encoding="utf-8" ?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
     android:src="@drawable/AndroidSplashScreen"
     android:gravity="clip_horizontal|clip_vertical" 
     />
    

    The "trick" is, to use "cliphorizontal|clipvertical" in one option and don't use any other option.
    Now, the image is scaled in (almost -> not 100%) correct aspect-ratio to fit the height and width of the device-screen.
    As I wrote, I have only ONE image in the drawable folder (Image in portrait-orientation, 776 KB, 640 x 1135).
    Note:
    As soon, as I include the hires-version (folder \drawable-xxhdpi\ parallel to drawable):
    - The phone is crashing (very) hard => reboot of device
    - The tablet then is using the hires-version of the image, but does not scale it correct (although, I have placed a
    SplashScreen.xml also in the \drawable-xxhdpi-folder)

    So... I finally have a solution, that works for me (not optimal, but acceptable for now -> maybe I later will try to optimize it).

    Finally many thanks to Daniel Luberda for the information's how to implement an Android splash-screen:-).

    I hope, this posting help some other developers...

    Tuesday, December 9, 2014 11:41 AM
  • User51359 posted

    I'm using the same splash screen pattern except that I have 1 splash screen and 2 possible starting routes. One of these routes have NoHistory = false because it's the main app. The problem I have is that you can get back to the splash screen due to actions in the main app such as clearing your credentials.

    I'm using Finish() when starting the splash activity but for some reason it is possible to use the back button to navigate back to the main app from the splash screen when you cleared your credentials.

    So yeah, Finish does not work because it doesn't remove the current Activity from the navigation stack.

    Thursday, December 11, 2014 7:58 AM
  • User21224 posted

    @Cybrosys The solution I posted is the proper way of doing things.

    Thursday, December 11, 2014 8:05 AM
  • User51359 posted

    I haven't been clear but my app needs to be able to switch the root page and remove any navigation history, which I've just achieved using multiple Activities, some with NoHistory = true and others with NoHistory = false. When navigation from a NoHistory = false activity to a NoHistory = true activity I need to call SetFlags on the Intent: SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

    This removes the NoHistory = false task from the navigation stack, so when back is pressed the app closes instead of going back to the previous activity.

    Thursday, December 11, 2014 8:42 AM
  • User2773 posted

    @Cybrosys? Take a look at this: http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l/12468288#12468288

    BTW: If you want to change background color of the splash screen:

    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/splash"
                  android:gravity="center"
                  android:layout_gravity="center"/>
            </item>
        </layer-list>
    
    Friday, December 12, 2014 12:36 AM
  • User51359 posted

    @DanielL? thanks for the info. I solved it by having NoHistory = true and when I start those activities with an Intent I set two flags on the intent: SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

    Friday, December 12, 2014 6:36 AM
  • User2773 posted

    @Cybrosys? Sorry, I didn't notice your second post.

    Friday, December 12, 2014 12:25 PM
  • User75222 posted

    About @Daniell solution, does anybody facing issues when hiding the application? It's throwing an exception on event "OnDestroy": UnhandledException_internal

    Here is an attached project wich reproduces the error.

    Friday, January 16, 2015 8:26 PM
  • User2773 posted

    @Alexandrem? Yeap (I think it's a bug). When you minimize app and reopen it through Android Task Manager. Take a look at: http://forums.xamarin.com/discussion/comment/89844/#Comment_89844

    Friday, January 16, 2015 8:30 PM
  • User75222 posted

    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

    Friday, January 23, 2015 10:56 AM
  • User28558 posted

    Thanks @Alexandrem? !

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

    Monday, January 26, 2015 4:13 PM
  • User71553 posted

    @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!

    Thursday, February 12, 2015 12:29 AM
  • User21224 posted

    @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.

    Thursday, February 12, 2015 1:25 AM
  • User71553 posted

    @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?

    Thursday, February 12, 2015 2:18 AM
  • User21224 posted

    API 17... target 21.

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

    Thursday, February 12, 2015 2:21 AM
  • User71553 posted

    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.

    Thursday, February 12, 2015 2:28 AM
  • User75222 posted

    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!

    Thursday, February 12, 2015 12:57 PM
  • User121194 posted

    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 : gmg.mike@outlook.com

    Thanks

    Thursday, April 16, 2015 5:22 PM
  • User50092 posted

    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 )

    Tuesday, June 2, 2015 6:53 AM
  • User50092 posted

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

    Tuesday, June 2, 2015 7:22 AM
  • User2773 posted

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

    Tuesday, June 2, 2015 4:09 PM
  • User89714 posted

    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.

    Wednesday, June 17, 2015 2:10 PM
  • User65389 posted

    @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...

    Wednesday, June 17, 2015 2:45 PM
  • User78799 posted

    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>
    
    Monday, November 30, 2015 2:58 PM
  • User17525 posted

    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?

    Monday, February 1, 2016 9:57 PM
  • User179286 posted

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

    Wednesday, March 9, 2016 8:32 PM
  • User132808 posted

    @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?

    Monday, April 4, 2016 10:48 PM
  • User208577 posted

    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.

    Sunday, April 10, 2016 5:05 PM
  • User89714 posted

    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.

    Thursday, April 21, 2016 10:36 PM
  • User31385 posted

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

    Thursday, April 21, 2016 11:01 PM
  • User76049 posted

    @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).

    Thursday, April 21, 2016 11:38 PM
  • User222192 posted

    This is the perfect solution thank you so much Daniel Luberda

    Wednesday, May 4, 2016 4:59 PM
  • User29221 posted

    https://developer.xamarin.com/guides/android/userinterface/creatingasplashscreen/

    Wednesday, July 27, 2016 1:03 PM
  • User246939 posted

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

    Wednesday, September 21, 2016 8:41 AM
  • User24588 posted

    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

      <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"/>
    
    Tuesday, October 4, 2016 11:45 PM
  • User118540 posted

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

    Tuesday, November 22, 2016 2:25 AM
  • User148199 posted

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

    Set MainLauncher = false on your MainActivity

    Tuesday, January 10, 2017 3:21 PM
  • User263383 posted

    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.

    Thursday, May 25, 2017 8:46 AM
  • User351573 posted

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

    Friday, October 6, 2017 6:47 AM
  • User200920 posted

    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

    Thursday, February 1, 2018 7:09 AM
  • User232295 posted

    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>
    
    Tuesday, May 22, 2018 7:52 PM
  • User272328 posted

    https://xamarinhelp.com/creating-splash-screen-xamarin-forms/

    Wednesday, May 23, 2018 3:35 AM
  • User342099 posted

    You do not need to create an SplashActivity, just add this line of xml to the current used style at MainActivity. ~~~~ @drawable/splashscreen true true ~~~~ In drawable and splash background (splashscreen.xml) ~~~~

    <bitmap
    
        android:src="@drawable/ic_launcher"
    
        android:tileMode="disabled"
    
        android:gravity="center"/>
    

    ~~~~ The icon is in drawable ic_lancher.png

    Wednesday, October 9, 2019 5:05 AM