In my Xamarin.Forms app, I use the below code for Android (which I found as a sample somewhere) to put up a splash screen on startup. This has two issues that I know of - one is that Xamarin.UITest doesn't work after this is used, the other is that the aspect ratio of the splash screen is not maintained if the user changes the orientation of the device.
Of those, the aspect ratio is the one that concerns me most, as I can work around the UITest one. Can anybody guide me on how to prevent Android changing the aspect ratio of the splash screen on rotating the device please? It would be great if it was a one liner added here, but that's probably over-optimistic :-)
namespace MyNamespace.Droid { [Activity( ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize, Icon = "@drawable/launchericon", Label = "MyAppName", LaunchMode = LaunchMode.SingleInstance, MainLauncher = true, NoHistory = true, ScreenOrientation = ScreenOrientation.FullSensor, Theme = "@style/Theme.Splash", WindowSoftInputMode = SoftInput.AdjustPan)] public class LaunchActivity : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); System.Threading.Thread.Sleep(3000); //Let's wait awhile... StartActivity(typeof(MainActivity)); } } }
Many thanks,
John H.
Answers
Are you setting the android:windowsBackground property of your theme directly to the image?
If so, try creating an xml file (splash.xml) in the drawable folder and add the reference your image from there:
Then modify your android:windowsBackground property to use the splash.xml instead:
This should maintain the aspect ratio on orientation change.
@LoriLalonde I do have a bitmap but it is bigger for some devices, I tried clip_horizontal but it only resizes the image horizontally, I just want 1 image in the center of the splash screen and the image to fit in the screen.
Any suggestions?
Can you use a layout axml instead splash.xml?