0

I need to show a splash screen that divides the screen at 50% with two colors. Furthermore, I need that this keep when you rotate the device from portrait to landscape. How can I achieve this layout?

  • Portrait:

Portrait

  • Landscape:

Landscape

For now, I have the following code, but here I have fixed sizes (440dp), and I need a solution with percentage size:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:height="440dp" android:gravity="fill_horizontal|top">
    <color android:color="@color/yellow"/>
  </item>
  
  <item android:height="440dp" android:gravity="fill_horizontal|bottom">
    <color android:color="@color/red"/>
  </item>
</layer-list>
Blarzek
  • 160
  • 12
  • 1
    Can't you show a vector drawable here and have it scale to the size of the screen? – Cheesebaron Oct 20 '20 at 05:15
  • @Blarzek You may not specify item width in percent, 'android:width' is defined here:https://developer.android.com/reference/android/R.attr.html#width, I find one thread that you can take a look:https://stackoverflow.com/questions/6061387/android-drawable-specifying-shape-width-in-percent-in-the-xml-file – Cherry Bu - MSFT Oct 20 '20 at 07:27
  • @Cheesebaron Could you tell me how to achieve this, please? I am a beginner at this... – Blarzek Oct 21 '20 at 08:30
  • @Cheesebaron A vector is really similar to this, because you need to specify a width and height in "dp" units... – Blarzek Oct 22 '20 at 09:27
  • @Blarzek If you still want to do, as [Aleadam](https://stackoverflow.com/questions/6061387/android-drawable-specifying-shape-width-in-percent-in-the-xml-file) said that you may need to create custom attrs. – Cherry Bu - MSFT Oct 28 '20 at 08:21
  • @CherryBu-MSFT Sorry, but I don't know how to do that... Could you guide me please? Thank you. – Blarzek Oct 29 '20 at 12:26
  • @Blarzek I have report this problem for better solution, please wait a minute. – Cherry Bu - MSFT Oct 30 '20 at 07:15

1 Answers1

0

You can use Layout to do this, like LinearLayout or AbsoluteLayout.

Firstly, create new layout name Splash-layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00ff00"/>
<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:background="#0000ff"/>

 </LinearLayout>

Then create splashActivity,make mainlauncher=true.

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

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Log.Debug(TAG, "SplashActivity.OnCreate");
        
        // Using Layout (green and blue)
        SetContentView(Resource.Layout.splash_layout);

        // Using a view, SplashView below, that draws its background (yellow and red)
        //SetContentView(new SplashView(this));

        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(8000); // Simulate a bit of startup work.
        Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
        StartActivity(new Intent(Application.Context, typeof (MainActivity)));
    }
}
Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16