Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AdjustResize Workaround for Xamarin.Forms

Forms 2.3.3+

Uses the new Platform Specifics feature

protected override void OnCreate(Bundle bundle)
{
	ToolbarResource = Resource.Layout.toolbar;
	TabLayoutResource = Resource.Layout.tabs;

	base.OnCreate(bundle);

	//Remove the status bar underlay in API 21+
	if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
	{
		Window.DecorView.SystemUiVisibility = 0;
		var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
		statusBarHeightInfo.SetValue(this, 0);
		Window.SetStatusBarColor(new Android.Graphics.Color(18, 52, 86, 255));
	}


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

	App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
}

Forms 2.3.2 and below

protected override void OnCreate(Bundle bundle)
{
	ToolbarResource = Resource.Layout.toolbar;
	TabLayoutResource = Resource.Layout.tabs;

	base.OnCreate(bundle);
	Window.SetSoftInputMode(SoftInput.AdjustResize);

	//Remove the status bar underlay in API 21+
	if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
	{
		Window.DecorView.SystemUiVisibility = 0;
		var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
		statusBarHeightInfo.SetValue(this, 0);
		Window.SetStatusBarColor(new Android.Graphics.Color(18, 52, 86, 255));
	}

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

This comment has been minimized.

Copy link

commented Oct 10, 2017

It was really helpful. Thank you.

@davidallen-bluemetal

This comment has been minimized.

Copy link

commented Apr 14, 2018

The _statusBarHeight field no longer exists in FormsAppCompatActivity for XF 2.5.0.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.