Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoDroid Splash Screen

How can I implement a simple "splash screen" on program startup? I am copying a SQLite DB and it can be a bit of a long process that is not UI "friendly" .

I would prefer not to use "java code".

TIA

like image 992
Fritz Avatar asked Jan 20 '26 19:01

Fritz


2 Answers

I recently solved this problem in the following way.

In the main activity I passed a parameter via the intent to set the number of milliseconds for which the splash screen would remain visible.

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

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        Intent i=new Intent();
        i.SetClass(this, typeof (Splash));
        i.PutExtra("Milliseconds", 3000);
        StartActivity(i);
    }

Then, in the second activity which I named "Splash" I retrieved the value and set a second thread to end the activity when the time had elapsed.

[Activity(Label = "Daraize Tech")]
public class Splash : Activity
{
    private int _milliseconds;
    private DateTime _dt;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        _milliseconds = Intent.GetIntExtra("Milliseconds", 1000);
        SetContentView(Resource.Layout.Splash);
        _dt=DateTime.Now.AddMilliseconds(_milliseconds);
     }

    public override void OnAttachedToWindow()
    {
        base.OnAttachedToWindow();

        new Thread(new ThreadStart(() =>
                                    {
                                    while (DateTime.Now < _dt)
                                        Thread.Sleep(10);
                                    RunOnUiThread( Finish );                                                   
                                    }
            )).Start();
    }

}
like image 199
Bob Powell MVP Avatar answered Jan 23 '26 09:01

Bob Powell MVP


Also see http://docs.xamarin.com/android/tutorials/Creating_a_Splash_Screen Really great tutorial.

It only takes about 10 lines of code :)

In a Styles.xml:

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

In your activity:

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

        // Create your async task here...
        StartActivity (typeof (Activity1));
    }
}
like image 44
Nicklas Møller Jepsen Avatar answered Jan 23 '26 08:01

Nicklas Møller Jepsen