'Xamarin Forms Android 12 SplashScreen backward compability
Im using xamarin and recently update my targetSdkVersion to 31 (Android 12) and wanted to use a SplashScreen, managed to get it working on a device with android 12, but for lower versions it doesnt show anything just a blank screen
styles.xml
<resources>
<style name="MainTheme" parent="Theme.MaterialComponents.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="snackbarStyle">@style/MySnackbar</item>
<item name="colorPrimary">#242424</item>
// Set the splash screen background, and icon background.
<item name="android:windowSplashScreenBackground">#242424</item>
<item name="android:windowSplashScreenIconBackgroundColor">#242424</item>
// Use windowSplashScreenAnimatedIcon to add either a drawable or an
// animated drawable. One of these is required.
// Use windowSplashScreenBrandingImage to add a drawable displayed at the bottom
// of the splash screen.
<item name="android:windowSplashScreenAnimatedIcon">@drawable/icone2</item>
<item name="android:windowSplashScreenBrandingImage">@drawable/logo</item>
</style>
<style name="MySnackbar" parent="Widget.MaterialComponents.Snackbar">
<item name="android:layout_margin">6dp</item>
</style>
</resources>
MainActivity
namespace App.Droid
{
[Activity(Label = "App", Icon = "@drawable/icone", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
UserDialogs.Init(this);
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
AndroidEnvironment.UnhandledExceptionRaiser += UnhandledExceptionRaiser;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
ZXing.Net.Mobile.Forms.Android.Platform.Init();
Forms.Init(this, bundle);
Popup.Init(this);
Platform.Init(this, bundle);
LoadApplication(new App());
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
if (App.Current.Resources.TryGetValue("PrimaryColor", out object objectStyle))
{
Window.SetStatusBarColor(Android.Graphics.Color.ParseColor(((Color)objectStyle).ToHex()));
Window.SetNavigationBarColor(Android.Graphics.Color.ParseColor(((Color)objectStyle).ToHex()));
}
else
{
Window.SetStatusBarColor(Android.Graphics.Color.Black);
Window.SetNavigationBarColor(Android.Graphics.Color.Black);
}
}
App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
}
}
}
Solution 1:[1]
Managed to do something, just doing an splash activity that wont show on android 12 and up
using Android.App;
using Android.Content;
using Android.OS;
using Xamarin.Forms;
namespace App.Droid
{
[Activity(Label = "App", Icon = "@drawable/icone", Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
Forms.Init(this, savedInstanceState);
}
protected override void OnResume()
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.S)
{
base.OnResume();
StartActivity(new Intent(Android.App.Application.Context, typeof(MainActivity)));
return;
}
base.OnResume();
StartActivity(new Intent(Android.App.Application.Context, typeof(MainActivity)));
}
public override void OnBackPressed() { }
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lucas Wolf |
