Is new Android 12 Splash Screen API supported in Xamarin Android?

Shira Hammann 41 Reputation points
2021-12-28T22:48:22.12+00:00

Our Xamarin Android application app splash screen is broken on devices running Android 12, I assume that's due to these changes in Android 12: https://proandroiddev.com/splashscreen-api-android-12-part-1-b2272e0ae98e. The Xamarin SDK for Android 12 is still in Beta, but I went ahead and installed it as explained by Ricko here - https://stackoverflow.com/questions/68156959/android-12-emulator-xamarin-android.

However, I don't know how to get access to the AndroidX splash screen compat library. I hoped maybe it would be included in Xamarin.AndroidX.AppCompat so I tried installing the latest version of that, 1.3.1.3, but even with that installed, if I try changing my reference in Styles.Xml from android:windowBackground to android:windowSplashScreenBackground, it isn't recognized.
I'm guessing that means that the new API isn't in that AppCompat package yet, or that it's the wrong package altogether.

Is there any way to fix the splash screen in a Xamarin Android app for Android 12 at this point? And is Xamarin.AndroidX.AppCompat the right library for me to keep checking for the API to show up? And is my test of changing the item name in Styles.xml a legitimate way to detect whether support is present? And is it documented anywhere which of the Android AppCompat capabilities are supported in Xamarin.AndroidX.AppCompat? Many thanks!

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Graham McKechnie 441 Reputation points
    2022-01-01T01:21:11.44+00:00

    @Shira Hammann

    I just downloaded your sample and it worked fine on my Pixel 6 and Pixel 3a (A12). Also worked on Pixel 4a (A11).

    It is a long time since I used VS 2019 so I didn't try that after you said it failed for you. If you are wanting to work with (A12) why aren't you using VS 2020 v17.0.4 or VS 2020 Preview 17.1.0 Prev 1.1? The same for your Nuget(s) they need upgrading too. You can install all the VS 2022's versions alongside your VS 2019, so it doesn't create any problems. Note you don't need Xamarin.Essentials so you can remove that package.

    Re my sample apps, I would presume the Java SDK 11 error is because you aren't using at least VS 2022, because there is now a new Microsoft folder for the SDK 11 which I don't know is even accessible from VS 2019.

    The article you referred to is using the same technique I've been using for the splash screen. Sorry, I may have misled you before, when I referred to mipmap. What I meant was that the mipmap ic_launcher_round icon should be copied from the mipmap folder into the drawable folder. You also need to be aware of the following if you try my examples https://github.com/xamarin/AndroidX/issues/444. You don't have to change anything as it is already done, the link is just informational.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Graham McKechnie 441 Reputation points
    2021-12-29T00:54:02.41+00:00

    @Shira Hammann
    Android is at androidx.core:core-splashscreen:1.0.0-alpha02 as of Dec 15th '21. Xamarin normally only release a Xamarin.Android version when Google go with a release version, so you could say, it is still coming. The probable name will be something like Xamarin.AndroidX.Core.SplashScreen

    However, If you have an existing splash screen that uses a layer-list with your app icon and your own background colour that doesn't require a separate activity then your Android 12 version will still work as before.

    If you want an example take a look at issue 6554 at https://github.com/xamarin/xamarin-android/issues/6554 where I uploaded a couple of zip files demonstrating the problem I'm having (unrelated to SplashScreens).

    0 comments No comments

  2. Shira Hammann 41 Reputation points
    2021-12-29T22:08:28.623+00:00

    Thank you very much!

    Regarding the version, I do see the core release announcement you're referring to here, but I'm confused what that means regarding the Android 12 release that has been downloaded to my Pixel. I apologize as I think this is an extreme newbie question, but given that my Pixel isn't enrolled in any kind of developer program, and I was prompted to download Android 12 there, I would expect Android 12 to be a stable release at this point, and I would expect that for any stable release that users are already being prompted to download, the androidx library to develop against that release would be available already... otherwise isn't it inevitable that many apps will be broken for users who download the new version, even if the developers are as up to date as is possible?

    Regarding the workaround suggested I am unsure why, but it doesn't seem to be working in our project. Our app does not have a layer-list xml, and does have a separate activity, but for testing purposes I just tried commenting out our separate activity, setting our MainActivity theme to the splash theme, and copying your layer-list xml, changing only the bitmap source and the background color. Unfortunately, it still isn't loading the icon consistently in Android 12 with those changes though (and there are other challenges):

    • On my Pixel with Android 12 installed, the background color from the xml does load as the splash screen, so I know the xml is being used, but the icon does not appear. It did appear a couple of the many times I ran the code when the code was erring out due to trying to call a nonexistent ActionBar, but most of the times when code was erring, and all the times since I removed the ActionBar call, it just shows the background, never the icon over it.
    • In my Pixel 2 Q 10 API 29 emulator, the splash screen works, but the icon isn't sized to the full screen, so it has a border of the background color around it. (Of course I also lost the ActionBar that I want to have in my MainActivity since I'm now inheriting from Theme.MaterialComponents.Light.NoActionBar whereas I actually need my MainActivity to inherit from android:Theme.Material.Light.DarkActionBar.)

  3. Graham McKechnie 441 Reputation points
    2021-12-30T01:53:01.81+00:00

    @Shira Hammann . Re your first paragraph, I think your question should be directed to Google, as they are the only ones qualified to answer your question re the use androidx.core:core-splashscreen:1.0.0-alpha02 or any other beta. See https://developer.android.com/guide/topics/ui/splash-screen

    There are actually two Splash screen APIs in Android 12 and what you get in Android 12 is the framework Api SplashScreen. My take on it is that the alpha splash screen version is an AppCompat version so that the new SplashScreen Api can also be applied to devices with versions lower than 12. At this point I haven't worked in Android Studio and androidx.core:core-splashscreen alpha, so I can't confirm how it works.

    The layer list type Splash screen is not a workaround. I've been using that type of splash screen for years. Its advantage is that it doesn't need an extra activity to implement it, (especially important if you use the NavigationComponent which is a Single Activity, multiple fragment architecture) therefore it is faster because it is only displayed during startup time, which you can see in a log file if you search for the string "Displayed". Sub one second times are attainable.

    The difference with Android 12 is that the splash screen is only displayed during a cold start, whereas the layer list type is also displayed during a warm start even if only very briefly. Therefore it is more than likely, I will swap to the AppCompat version as soon as Xamarin.Android releases their version of that Nuget.

    I've got no idea why it is not working in your app, however, I'd suggest building a test app from scratch so you get a proper understanding of how it is meant to work. I don't know why you have lost your action bar, but it has got nothing to do with NoActionBar theme. If you look at my styles.xml you will find two themes AppTheme and AppTheme.Splash

    style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"
    and
    style name="AppTheme.Splash" parent="Theme.MaterialComponents.Light.NoActionBar">

    Then check the MainActivity.cs where in OnCreate(), I set the theme from AppTheme.Splash back to the AppTheme with SetTheme(Resource.Style.AppTheme);

    My app has an action bar (Toolbar), where else would the single menu item be coming from? If in any doubt you should always check the androidmanifest.xml in the \obj\android\manifest to see what is your generated androidmanifest.xml.

    If you want to check, look at the other version of my test app MaterialAlertBuilderNavGraph (a NavigationComponent version). It also has two themes the latest version which you can find here in the thread AppCompatDialogFragment not display OK button on Android12 devices which doesn't have any layer list stuff. It displays an automatic splash screen on an Android 12 device only. All other devices just get a white background at startup.

    I forgot to add, the icon (in the first example) must be one from your mipmap folder - the round icon.


  4. Shira Hammann 41 Reputation points
    2022-01-01T00:02:32.14+00:00

    Thanks much!

    I have read through the official Android documentation and have tried several times, following different instructions from several different websites, to make this work in a new project. Most recently, I followed the instructions here - https://progrunning.net/best-way-to-create-a-splash-screen-in-xamarin-forms-android-project/.

    The splash screen in my sample projects consistently works fine on versions of Android prior to 12 (e.g. my Pixel 2 Q 10.0 - API 29 emulator), but it does not work on my physical Pixel 3a device running Android 12. There, only the splash background color shows, with no icon. Here is one of my test projects created from scratch: https://drive.google.com/file/d/1AsNG3YfyktKKV2Sk7NMeCm6WQLib3GdN/view?usp=sharing

    Also, regarding mipmap, not sure what I'm doing wrong, but I find that the splash_screen.xml background color is only respected if I reference the icon from drawable - if I reference it from mipmap, then on Android 10 the app's main screen appears as the first thing I see - there is no loading screen - and on Android 12 I see a black splash screen without any icon.

    I am also unfortunately not sure how to build either of your sample projects on my machine in order to confirm whether that splash screen works on my Pixel 3a. I assume the error I'm getting indicates that my setup to use the Android 12 beta SDK is not correct - error is as follows:

    "Java SDK 11.0 or above is required when using $(TargetFrameworkVersion) v12.0. Note: the Android Designer is incompatible with Java SDK 11.0: https://aka.ms/vs2019-and-jdk-11."
    

    I followed the instructions to install Microsoft's build of JDK 11 (I installed 11.0.13+8(x64), and to configure Visual Studio 2019 to look for the Android SDK at C:\Program Files (x86)\Android\android-sdk, and I confirmed that your projects both target Android 12.0 (S), but the error persists.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.