How to add a splashscreen text in a layer-list in Xamarin forms

Viewed 2045

!I've implemented this splashscreen in android using android studio, but I have switched to xamarin forms for cross-platform, so I want to implement a similar splashscreen in xamarin forms]1

So far I have been able to add the logo, but I need to add the text, how do I do that?

Here is my splash.xml code in xamarin forms:

   <?xml version="1.0" encoding="utf-8" ?>
   <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item>
    <color android:color="@color/colorBlack"/>
    </item>
    <item>
     <bitmap
        android:src="@drawable/ic_logo"
        android:tileMode="disabled"
        android:gravity="center"/>
     </item>


</layer-list>
1 Answers

The default SplahScreen does not support adding text.

A quick way to solve it is to include those version info inside your image.

Another way is create a Activity that represents your splash screen.Then when your SplashActivity is created you start a timer with 3 seconds duration. When those 3 seconds have passed you simply start the main activity for your app.

Layout for SplashActivity :

  <RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center_vertical" 
   android:background="#fff"
   android:id="@+id/splashview">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src = "@drawable/splash_logo"
    />

   <TextView
        android:id="@+id/txtAppVersion"
        android:text="test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="30dp"
        android:layout_marginBottom="30dp"
        android:textColor="#000"
        android:textSize="24sp"
        android:gravity= "bottom"
        android:layout_centerHorizontal="true"/>
    </RelativeLayout>

And code behind:

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher =true,NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
    static readonly string TAG = "X:" + typeof (SplashActivity).Name;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Splash);

        FindViewById<TextView>(Resource.Id.txtAppVersion).Text = $"Version {PackageManager.GetPackageInfo(PackageName, 0).VersionName}";

    }

    // Launches the startup task
    protected override void OnResume()
    {
        base.OnResume();
        Task startupWork = new Task(() => { SimulateStartup(); });
        startupWork.Start();
    }

    // Prevent the back button from canceling the startup process
    public override void OnBackPressed() { }

    // Simulates background work that happens behind the splash screen
    async void SimulateStartup()
    {
        Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
        await Task.Delay(5000); // Simulate a bit of startup work.
        Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
        StartActivity(new Intent(Application.Context, typeof(MainActivity)));
    }
}

I uploaded a sample project here and you can check.

Related