Accommodating The On Screen Keyboard in Xamarin Forms

Most, if not all apps, will require an entry field for user input. When you are on a mobile device, selecting an entry field will cause the onscreen keyboard to appear and depending upon the platform and app settings it can cause different things to happen to your layout.


Xamarin Forms Page

As a starting point, here is a simple Xamarin Forms page that creates 3 sections each with an entry field. A sample project SoftInput is available if you want to see the completed code.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=""
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="50" />
        <StackLayout Grid.Row="0" BackgroundColor="#1E73BE">
            <Entry />
        <StackLayout Grid.Row="1" BackgroundColor="#7030A0">
            <Entry />
        <StackLayout Grid.Row="2" BackgroundColor="#00B050">
            <Entry />


Android will commonly refer to the onscreen keyboard as SoftInput. By default, with Xamarin Forms, it will use AdjustPan, which will move the entire screen upwards to make room for the keyboard. This has the unfortunate side effect of making the top part of the screen unreachable when the keyboard is showing.


If you want the screen to resize you need to implement two pieces of code. First, in the MainActivity set the SoftInputMode to AdjustResize.


Next, due to a bug in Android that they won’t fix, we need to implement a workaround.

public class AndroidBug5497WorkaroundForXamarinAndroid

    // For more information, see
    // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

    // CREDIT TO Joseph Johnson ( for publishing the original Android solution on

    public static void assistActivity(Activity activity)
        new AndroidBug5497WorkaroundForXamarinAndroid(activity);

    private Android.Views.View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497WorkaroundForXamarinAndroid(Activity activity)
        FrameLayout content = (FrameLayout)activity.FindViewById(Android.Resource.Id.Content);
        mChildOfContent = content.GetChildAt(0);
        ViewTreeObserver vto = mChildOfContent.ViewTreeObserver;
        vto.GlobalLayout += (object sender, EventArgs e) => {
        frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;

    private void possiblyResizeChildOfContent()
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious)
            int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;

            frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;

            usableHeightPrevious = usableHeightNow;

    private int computeUsableHeight()
        Rect r = new Rect();
        return (r.Bottom - r.Top);

Then add this just below the SetSoftInputMode line.


Alternative Method

The above method, does sometimes have issues, when clicking the back button. As such, this is an alternative method to achieving the same thing, without the above workaround. Place this code in your MainActivity.cs, after LoadApplication.


Then, the status bar will have a few issues, so you will need to do this to compensate.

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(0,0,0, 255)); // Change color as required.


When in landscape however, Android goes to FullScreen Mode as seen below.


You can disable this by implementing a custom renderer for your Entry fields to set the ImeOptions flag to NoExtractUi.

[assembly: ExportRenderer(typeof(Entry), typeof(EntryRender))]
namespace SoftInput.Droid.Render
    using Android.Views.InputMethods;
    using Xamarin.Forms.Platform.Android;

    public class EntryRender : EntryRenderer
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)

            if (Control != null)
                Control.ImeOptions = (ImeAction)ImeFlags.NoExtractUi;

By implementing this, you will get something similar to the experience in Portrait.


The default iOS experience is one of the better experiences with the keyboard. By default it will AdjustPan upwards but you can scroll through all the content. However to get this scroll you must implement the ScrollView around the scrollable content. This has no effect on Android.

   // Existing controls in here


You can place controls outsize of the ScrollView to ensure they stay in view.

If you want to shrink the iOS view, the same as Android, you can do this via a custom renderer. Have a look at the SoftInput KeyboardRender.



UWP refers to the Soft Keyboard as an Input Pane. By default UWP does an AdjustPan, the same as Android, with no scroll capability.


In order to do a resize, I put together this code in your MainPage.cs

public MainPage()

    LoadApplication(new SoftInput.App());
    _originalHeight = ApplicationView.GetForCurrentView().VisibleBounds.Height;

    InputPane.GetForCurrentView().Showing += MainPage_Showing;
    InputPane.GetForCurrentView().Hiding += MainPage_Hiding;

private double _originalHeight;
private void MainPage_Hiding(InputPane sender, InputPaneVisibilityEventArgs args)
    this.Height = _originalHeight;
    this.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Stretch;

private void MainPage_Showing(InputPane sender, InputPaneVisibilityEventArgs args)
    this.Height = _originalHeight - args.OccludedRect.Height;
    this.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;

This results in this effect, but I must warn you it would need enhancing further to support rotation.


If anyone else has any tips and tricks for the on screen keyboard on any platform, please let me know in the comments below.


  1. Tony Casey

    Hey Adam,

    This is fantastic, however, on Android, if I am on a second screen in the stack and the editor shows, if I press back to the root page then a frame is left on the screen blocking half the root page.

    Any ideas?

    1. Adam Pedley

      Came across this problem myself. There are 2 ways to correct it. You need to either cause the page to update its screen size every time a page reloads. Or, tap into the back button and cause it to close the keyboard first, then move back.

      I prefer the latter, where a back button closes the keyboard. Then a second back button moves the page back.

  2. Rafael

    Can you indicate me where to look to solve the same problem but upward and rezise all content page with entry’, labels, buttons and image ?? Like the login page from facebook app ?

    1. Adam Pedley

      Not sure I completely understand the question. The resizing as mentioned above will resize the layout when the keyboard is shown. It won’t resize the elements height inside the layout though, except for an image.

      1. Rafael

        I just have a problem on my login page, that is when i write the username and password the login button that is underneath will be covered by the keyboard and i cant pull up my layout to see the login button.

  3. Hakan

    Hi Adam Pedley,

    Although the solution works for the latest Android phones, when I tried it with Samsung Galaxy S3 (Android 4.3), the space under the keyboard does not redraw after the keyboard is dismissed. That part stays white. You can try it in a simulator, result is the same. How can that be prevented?

  4. Robin

    This is a really great article. I have a complex page with a custom header and so I used a combination of these to get it working. Adam, thanks for writing comprehensive posts that show the whole problem and multiple ways to fix it! THANKS!

  5. Gilbok Lee

    It works fine on my Nexus 5, 6.
    But it doesn’t work on the Samsung Galaxy S8 or Note 5.
    Samsung might be a problem.

  6. Philipp

    I would love Xamarin to provide a simple XF Chat UI that actually works. Keyboard input, resizing of the ListView (and keeping the position), Multi-line text input (Entry resizes on both line breaks and word wrapping) to just name the most obvious ones – this exposes a myriad of issues in the platform. I’ve literally spent weeks on a UI that I would build in WPF Xaml in a matter of minutes… :/