Creating a dynamic xamarin splash screen for android app

Jesse Knott 691 Reputation points
2021-09-21T05:18:43.39+00:00

In my app at the moment, I have a splash screen that shows a logo with as the app starts up.
I am making a free version of the app, and want to have a modified splash screen for that version. The way I manage the splash screen is to make a custom startup activity that then calls the main application activity after displaying the screen.

The problem I am having is that for some reason I am getting an exception that says several things.

Android.Content.Res.Resources+NotFoundException: File res/layout/scaledsplash.xml from drawable resource ID #0x7f0c00b7 ---> Android.Views.InflateException: Class not found ImageView ---> Java.Lang.ClassNotFoundException: Didn't find class "ImageView" on path: DexPathList

I have tried cleaning, and even scrubbing the obj folders, but it seems it's something more than an incorrect resource id in a precompiled file.

Here is the code I have

SplashActivity.cs

  [Activity(NoHistory = true,
                Theme = "@style/SplashScreen",
                MainLauncher = true)]
    public class SplashActivity : FormsAppCompatActivity
    {
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            try
            {
                base.OnCreate(savedInstanceState);

                this.SetContentView(Resource.Layout.ScaledSplash);

                ImageView image = FindViewById<ImageView>(Resource.Id.ScaledSplashImage);

    #if FREE_VER
                image.SetImageResource(Resource.Drawable.FreeAppIconV2);
    #else
                image.SetImageResource(Resource.Drawable.AppIconV2);
    #endif
                //Build the database if needed, and populate the starting data set if necessary 
                await DBHelper.CreateDBTables().ConfigureAwait(true);
                await DBHelper.InitDBStore().ConfigureAwait(true);

                //Give the splash screen a few seconds to show before continuing on.
                await Task.Delay(TimeSpan.FromSeconds(5d)).ConfigureAwait(true);

                //Star the main app.
                await InitMainApp();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"SplashActivity.cs->Main()->{ex.Message}{System.Environment.NewLine}{ex.InnerException}{System.Environment.NewLine}{ex.StackTrace}");
            }
        }

        private async Task InitMainApp()
        {
            StartActivity(typeof(MainActivity));
        }


values/Styles.xml

<style name="SplashScreen" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@layout/ScaledSplash</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>


layout/ScaledSplash.xml

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ScaledSplashImage"
    android:contentDescription="ImagePane" 
  />

Here is the main activity to start the main app once the splash screen is done showing.

MainActivity.cs


   [Activity(Label = "ShootersToolbox", Icon = "@drawable/AppIconSM",
        Theme = "@style/MainTheme",
              LaunchMode = LaunchMode.SingleInstance,
                MainLauncher = false,
              WindowSoftInputMode = SoftInput.AdjustPan,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : FormsAppCompatActivity
    {
        #region Methods

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            //Added for the camera API
            Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
            TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            Rg.Plugins.Popup.Popup.Init(this);
            Xamarin.Forms.Forms.Init(this, savedInstanceState);

            DisplayCrashReport();

            MobileAds.Initialize(ApplicationContext); 

            Xamarin.Forms.FormsMaterial.Init(this, savedInstanceState);
            Syncfusion.XForms.Android.PopupLayout.SfPopupLayoutRenderer.Init();

            CrossCurrentActivity.Current.Init(this, savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);


            LoadApplication(new App());
        }

        #endregion Methods

        #region _r8 linker bug_

        /// <summary>
        /// Repairs problem with r8 linker not linking in these API functions...
        /// </summary>
        private const bool falseflag = true;

        private static void LinkerPleaseInclude()
        {
            if (falseflag)
            {
                _ = new ButtonBarLayout(null, null);
                _ = new FitWindowsFrameLayout(null);
                _ = new AlertDialogLayout(null);
                _ = new DialogTitle(null);
            }
        }

        #endregion _r8 linker bug_
    }
}

Any ideas or pointers most appreciated!!

Cheers!

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

Accepted answer
  1. Anonymous
    2021-09-21T07:30:00.087+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Please open your styles.xml, then remove the <item name="android:windowBackground">@layout/ScaledSplash</item> line,

    Then this error will be disappeared.

    ================
    Update sample=====================

    If you want to achieve the following style splash screen.

    133810-image.png

    you need to change the SplashScreen style like following code.

       <style name="SplashScreen" parent="Theme.AppCompat.Light.NoActionBar">  
         
         
           <item name="android:windowIsTranslucent">true</item>  
           <item name="android:windowNoTitle">true</item>  
           <item name="android:windowFullscreen">true</item>  
           <item name="android:windowContentOverlay">@null</item>  
           <item name="android:windowActionBar">true</item>  
            
         </style>  
    

    Then change the ScaledSplash.xml layout like following code.

       <?xml version="1.0" encoding="utf-8"?>  
       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
           android:gravity="center"  
           android:layout_width="match_parent"  
           android:layout_height="match_parent">  
        <ImageView  
            xmlns:android="http://schemas.android.com/apk/res/android"  
             
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:id="@+id/ScaledSplashImage"  
            android:src="@drawable/splash_screen"  
            android:contentDescription="ImagePane"   
          />  
       </RelativeLayout>  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.