Background image on Android Activity, shortly visible after app starts ?

FonagerFonager DKMember ✭✭

Hi everyone

As my Android (Forms) app has quite a long startup time, we decided to insert at logo on the main activity background, by using a custom xml :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/white"/>
    <item>
        <bitmap
            android:id="@+id/background_layout"
            android:gravity="center"
            android:src="@drawable/icon"/>
    </item>
</layer-list>

And i reference the above xml in my theme :

<item name="android:windowBackground">@drawable/startup_background</item>

It works great, but my problem is, that when I push a new ContentPage onto a NavigationPage, you slightly see the logo, before the actual Content of the new Page has been rendered.

Is it somehow possible to remove the reference to this background xml at runtime, so its only visible at startup ? And how do I find the Id of the layout holding the Forms "framework" ? Wouldnt it be possible to remove it after the call to :

LoadApplication (new App ());

Maybe I'm doing this the wrong way? Please let me know how you guys tackle this ?

Cheers,
Kent Fonager, Mesh ApS

Best Answer

Answers

  • FonagerFonager DKMember ✭✭

    Hi Alex

    Thank you so much for answering, it works like a charm :-)

    Cheers,
    Kent

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    You might be interested in this other thread on the same subject. A couple of us have had some fairly active back-and-forth about styles, issues, fixes etc.

    https://forums.xamarin.com/discussion/83566/android-xamarin-forms-set-splash-screen-while-xamarin-forms-initalizes

  • Chris_AChris_A Member

    This is by far the most simple, and well working example I have come across. Thank you!

  • Hikari91Hikari91 ITMember ✭✭
    edited March 15

    @AlexDunn said:
    I've always done this by creating a separate Activity for the splash page that has the image set up, then calls to start MainActivity. Note that an activity will not be removed from View until the other has finished creating, so the splash will stay up until Xamarin.Forms finishes initializing.

     [Activity(Label = "Sendtell", MainLauncher = true, NoHistory = true, Theme = "@style/Theme.Splash",
         ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
        public class SplashActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                var intent = new Intent(this, typeof(MainActivity));
                StartActivity(intent);
                Finish();
            }
        }
    

    the theme:
    ```

    > > @drawable/Splash > > true > false > false > true > #FFFFFF >

    ```

    Note that I also set the SplashActivity to MainLauncher and also set NoHistory so the user can't travel back to it. Don't forget to remove the MainLauncher tag from your MainActivity too.

    Hi,
    I've tried your solution but when StartActivity(intent); is called, the new activity is brought to the foreground and the splashscreen is hide.

    There are the attribute of my MainActivity:

    [Activity(Label = "@string/AppName", Icon = "@mipmap/icon", Theme = "@style/MainTheme",
        MainLauncher = false,
        NoHistory = true,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
        WindowSoftInputMode = SoftInput.StateAlwaysHidden,
        LaunchMode = LaunchMode.SingleInstance)]
    [IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault }, DataMimeTypes = new[] { "*/*" })]
    

    Thank you :smile:

Sign In or Register to comment.