Forum Xamarin.Android

How to maintain splash screen aspect ratio if device is rotated?

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

  • LoriLalondeLoriLalonde CAInsider, University, Developer Group Leader ✭✭✭

    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:

    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/splashScreen"
        android:gravity="center" />
    

    Then modify your android:windowsBackground property to use the splash.xml instead:

    <style name="Theme.Splash" parent="android:Theme">
        <item name="android:windowBackground">@drawable/splash</item>
        <item name="android:windowNoTitle">true</item>
      </style>
    

    This should maintain the aspect ratio on orientation change.

  • VictorHGarciaVictorHGarcia USUniversity ✭✭

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

  • SuatKorkmazSuatKorkmaz USMember ✭✭✭

    @LoriLalonde said:
    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:

    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/splashScreen"
        android:gravity="center" />
    

    Then modify your android:windowsBackground property to use the splash.xml instead:

    <style name="Theme.Splash" parent="android:Theme">
        <item name="android:windowBackground">@drawable/splash</item>
        <item name="android:windowNoTitle">true</item>
      </style>
    

    This should maintain the aspect ratio on orientation change.

    Can you use a layout axml instead splash.xml?

Sign In or Register to comment.