Jason
Jason

Reputation: 13

Display different splash screens depending on conditions

What I want to do is display a splash screen of a daytime picture if it is between certain times, then a night time picture if it is not in between those times. First, I am wondering if this is even possible. I am using Xamarin Forms for this project, but even having the code for iOS or Android would help. For Android, the code that I have right now just displays a splash screen. Im just unsure if there is anyway to have this change.

Styles.axml

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

MainActivity.cs

[Activity(Theme = "@style/Theme.Splash", //Indicates the theme to use for this activity
         MainLauncher = true, //Set it as boot activity
         NoHistory = true)] //Doesn't place it in back stack
public class SplashActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        System.Threading.Thread.Sleep(3000); //Let's wait awhile...
        this.StartActivity(typeof(MainActivity));
    }
}

Upvotes: 0

Views: 2147

Answers (2)

Bryan
Bryan

Reputation: 15166

Firstly, I would strongly urge you to follow this tutorial for creating splash screens the correct way. I do not know how well this would translate to Xamarin (as I have not worked with the platform), but the same concepts should apply.


From the answer you posted, it looks like you are just trying to change the splash screen based on whether it is day or night. This can be done easily with night or notnight resource qualifiers.

For example, if you use a drawable for your splash screen, you can create a drawable-night folder. Then, place the drawable for your day splash screen in the drawable folder, and a drawable for your night splash screen in the drawable-night folder (make sure both drawables have the same name).

Upvotes: 1

Jason
Jason

Reputation: 13

If anyone is interested or has this issue in the future, this is what I ended up doing in Android (haven't looked at iOS yet). I think it is as close to having 2 different splash screens based on times as I can get. It initially loads a black screen, then based on the time of day, it loads either the daytime or nighttime picture:

In styles.xml

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

<style name="Theme.Splash2" parent="android:Theme">
    <item name="android:windowBackground">@drawable/splash2</item>  <!--Nighttime-->
    <item name="android:windowNoTitle">true</item>
</style>

<style name="Theme.Splash3" parent="android:Theme">
    <item name="android:windowBackground">@drawable/splash3</item>  <!--All black-->
    <item name="android:windowNoTitle">true</item>
</style>

In MainActivity.cs

 [Activity(Theme = "@style/Theme.Splash3", MainLauncher = true, NoHistory = true)]
public class SplashActivity3 : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        System.Threading.Thread.Sleep(1);

        if ((System.DateTime.Now.Hour >= 6) && (System.DateTime.Now.Hour <= 18))
        {
            StartActivity(typeof(SplashActivity1));
        }

        else
        {
            StartActivity(typeof(SplashActivity2));

        }
    }


    [Activity(Theme = "@style/Theme.Splash", NoHistory = true)]
    public class SplashActivity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Timer timer = new Timer();
            timer.Interval = 3000; // 3 sec.
            timer.AutoReset = false; // Do not reset the timer after it's elapsed
            timer.Elapsed += (object sender, ElapsedEventArgs e) =>
            {
                StartActivity(typeof(MainActivity));
            };
            timer.Start();


        }
    }

    [Activity(Theme = "@style/Theme.Splash2", NoHistory = true)]
    public class SplashActivity2 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Timer timer = new Timer();
            timer.Interval = 3000; // 3 sec.
            timer.AutoReset = false; // Do not reset the timer after it's elapsed
            timer.Elapsed += (object sender, ElapsedEventArgs e) =>
            {
                StartActivity(typeof(MainActivity));
            };
            timer.Start();

        }
    }

Upvotes: 0

Related Questions