user1 user1 - 5 months ago 257
Android Question

How can I hide the navigation bar BEFORE the first page has displayed?

I am building a

Xamarin Android
app using
Xamarin.Forms
my
App
class is really simple and contains one page like so:

public App()
{
// The root page of your application
MainPage = new NavigationPage(new ContentPage()
{
Title = "My First Page!",
Content = new Label()
{
Text = "Test Page!",
TextColor = Color.White,
FontSize = 25,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
}
});
}


and my
MainActivity
class like so:

[Activity(Label = "GridTest", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}


When I launch the application though the
NavigationBar
is displayed with the word "GridTest" displayed in the top left which looks rubbish, shown below:

This is the NavigationBar I would like to hide.

enter image description here

After this my application loads with the navigation bar fine. Shown below:

I would like to keep this Navigation Bar

enter image description here

So my question is. How do I hide the Android
NavigationBar
that is displayed BEFORE the initial page has loaded?

SIDE NOTE:

I have tried
NavigationPage.SetHasNavigationBar(page,false);
but this does not work. This hides the
NavigationBar
for the first page shown below.
enter image description here

which I would like to keep! This also does not hide the "GridTest" navigation bar I am looking to hide (shown in screenshot 1)

To reiterate... I am looking to hide the INITIAL navigation bar. provided by the Android
MainActivity
NOT the
Xamarin.Forms
navigation bar

Answer

The main activity which will load the first time the application starts.

[Activity(Label = "testapp", MainLauncher = true, Theme = "@style/MyTheme", ScreenOrientation = ScreenOrientation.Portrait)]
    public class SplashActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
           // SetContentView(Resource.Layout.LaunchScreen); //Set a splash screen here if you wish
            Java.Lang.Runnable runnable = new Java.Lang.Runnable(() =>
            {
                Intent i = new Intent(this, typeof(MainActivity));
                StartActivity(i);
            });

            new Handler().PostDelayed(runnable, 1000);
        }
    }

The theme for that activity must be saved to Resources/drawable/styles.xml and is

<resources>
  <style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowNoTitle">true</item>
  </style>
</resources>

This SplashActivity will load and call MainActivity

and your MainActivity will look like this:

[Activity(Label = "GridTest", Icon = "@drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
}

You dont need to set the Theme here and it will work as before. Another Important note about the MainActivity is you MUST remove MainLauncher = true