title: Xamarin Android: Issues with Custom Keyboard Of Custom Renderer - Appears Underneath Controls, Keys Not Pressable, etc
body: <p>I am creating a custom control that has a custom keyboard. I have faced several issues.</p>
  <p>I am using Visual Studio Community 2019, and I'm using Android_Accelerated_x86_Oreo (Android 8.1 API-27) as my Android emulator.</p>
  <p>In summary,</p>
  <p>I need to style Entry differently, hence I create a derived class from it, called TextBoxEntry.</p>
  <p>I create all the controls programmatically, in MainPage.cs, hence, MainPage.xaml is blank (well, almost).</p>
  <p>Android specific codes, namely the renderer, are crammed into MainActivity.cs.</p>
  <p>And lastly, the custom keyboard, is defined under Resources/xml/keyboard.xml.</p>
  <p>I have not been able to work out these issues (3 of 5 solved):</p>
  <ol>
  <li><p>Why does the custom keyboard of the custom renderer appear underneath the controls?</p>
  </li>
  <li><p>When the keyboard appears, I need to get its height, so that I could modify the scrollview dimension accordingly. Unfortunately, doing this inside ControlOnFocusChanged() [EditText.FocusChange event handler] just doesn't work, because at that point in time, the keyboard still isn't displayed yet, hence, its height is 0. Or should I do it in some other event?
  <strong>Should use ViewTreeObserver.IOnGlobalLayoutListener instead.</strong></p>
  </li>
  <li><p>The keys in the keyboard are not pressable. Sometimes, the keyboard goes into hiding. I guess when I press the keys, the actual entry loses focus. Anyway, where do I actually go wrong?</p>
  </li>
  <li><p>The last column of the scrollview, as well as the the last row, have their TextBoxEntries clipped off. Again, I couldn't figure out what goes wrong.
  <strong>THIS IS SOLVED BY SETTING MARGIN TO 1.</strong></p>
  </li>
  <li><p>Inside ControlOnFocusChange(), a_height gives 1280, while b_height gives 342. The scrollview takes more than half of the entire screen, but its height is far from half of the entire screen. Are these two shown in different units?
  <strong>According to <a href="https://stackoverflow.com/questions/33214768/find-our-the-default-height-of-the-controls-in-xamarin-forms">Find our the default height of the controls in Xamarin Forms</a>, it seems that both Heights use different units... OK, this can be solved by some conversion.</strong></p>
  </li>
  </ol>
  <p>So, here is all the relevant files' content:</p>
  <p>MainPage.xaml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  &lt;ContentPage xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
               x:Class=&quot;CustomKeyboard.MainPage&quot;/&gt;
  
  </code></pre>
  <p>MainPage.xaml.cs:</p>
  <pre><code>using Xamarin.Forms;
  
  namespace CustomKeyboard
  {
      public class TextBoxEntry : Entry
      {
          public ScrollView ScrollView { get; set; } = null; // So that I could easily get its reference and work on it...
      }
      public partial class MainPage : ContentPage
      {
          AbsoluteLayout layout = null;
          ScrollView scrollview = null;
          Grid grid = null;
  
          public MainPage()
          {
              InitializeComponent();
              BuildControls();
              BuildTextBoxes();
          }
          private void BuildControls()
          {
              layout = new AbsoluteLayout();
              scrollview = new ScrollView();
              grid = new Grid();
              layout.Children.Add(scrollview, new Rectangle(0.5d, 0d, 0.95d, 0.85d), AbsoluteLayoutFlags.All);
              scrollview.BackgroundColor = Color.Yellow;
              scrollview.Padding = 5;
              scrollview.Content = grid;
              Content = layout;
          }
          private void BuildTextBoxes()
          {
              for (var i = 0; i &lt; 50; ++i)
              {
                  if (i % 5 == 0)
                  {
                      var l = new Label()
                      {
                          Text = (i / 5).ToString(),
                          HorizontalTextAlignment = TextAlignment.Center,
                          VerticalTextAlignment = TextAlignment.Center,
                      };
                      grid.Children.Add(l, 0, i / 5);
                  }
                  else
                  {
                      var r = i / 5;
                      var c = i % 5;
                      var e = new TextBoxEntry();
                      e.Margin = 1; // THIS SOLVES ISSUE 4
                      e.ScrollView = scrollview;
                      grid.Children.Add(e, c, r);
                  }
              }
          }
      }
  }
  
  </code></pre>
  <p>MainActivity.cs:</p>
  <pre><code>using Android.App;
  using Android.Content;
  using Android.Content.PM;
  using Android.Graphics;
  using Android.Graphics.Drawables;
  using Android.InputMethodServices;
  using Android.OS;
  using Android.Support.Design.Widget;
  using Android.Views;
  using Android.Widget;
  
  using Java.Lang;
  
  using System.Threading.Tasks;
  
  using Xamarin.Forms;
  using Xamarin.Forms.Platform.Android;
  using Xamarin.Essentials;
  
  using CustomKeyboard;
  using CustomKeyboard.Droid;
  
  [assembly: ExportRenderer(typeof(TextBoxEntry), typeof(TextBoxEntryRenderer))]
  
  namespace CustomKeyboard.Droid
  {
      [Activity(
          Label = &quot;CustomKeyboard&quot;,
          Icon = &quot;@mipmap/icon&quot;,
          Theme = &quot;@style/MainTheme&quot;,
          MainLauncher = true,
          ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation
      )]
  
      public class MainActivity : FormsAppCompatActivity
      {
          internal static MainActivity Instance { get; private set; }
          protected override void OnCreate(Bundle savedInstanceState)
          {
              Instance = this;
  
              base.OnCreate(savedInstanceState);
  
              Xamarin.Essentials.Platform.Init(this, savedInstanceState);
              Forms.Init(this, savedInstanceState);
              LoadApplication(new App());
          }
      }
  
      public class TextBoxEntryRenderer : ViewRenderer&lt;TextBoxEntry, TextInputLayout&gt;
      {
          public KeyboardView mKeyboardView;
          ViewGroup activityRootView;
          protected EditText editText =&gt; Control.EditText;
  
          public TextBoxEntryRenderer(Context context) : base(context) { }
  
          protected override TextInputLayout CreateNativeControl()
          {
              #region Add keyboard
              var activity = MainActivity.Instance as Activity;
              activity.Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);
  
              var rootView = activity.Window.DecorView.FindViewById(Android.Resource.Id.Content);
              activityRootView = ((ViewGroup)rootView).GetChildAt(0) as ViewGroup;
  
              mKeyboardView = new KeyboardView(MainActivity.Instance, null);
              mKeyboardView.PreviewEnabled = false; //Removes magnified key popups on key press
              mKeyboardView.Visibility = ViewStates.Gone;
              mKeyboardView.Keyboard = new Android.InputMethodServices.Keyboard(Context, Resource.Xml.keyboard);
              mKeyboardView.Key += (sender, e) =&gt;
              {
                  long eventTime = JavaSystem.CurrentTimeMillis();
                  DispatchKeyEvent(new KeyEvent(eventTime, eventTime, KeyEventActions.Down, e.PrimaryCode, 0, 0, 0, 0, KeyEventFlags.SoftKeyboard | KeyEventFlags.KeepTouchMode));
  
                  Task.Delay(1);
              };
              Android.Widget.RelativeLayout.LayoutParams layoutParams =
                  new Android.Widget.RelativeLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent); // or wrap_content
              layoutParams.AddRule(LayoutRules.AlignParentBottom);
              activityRootView.AddView(mKeyboardView, layoutParams);
              #endregion
  
              var _editText = new EditText(Context);
              _editText.SetTextColor(Android.Graphics.Color.Black);
              var textInputLayout = new TextInputLayout(Context);
              textInputLayout.AddView(_editText);
              return textInputLayout;
          }
          protected override void OnElementChanged(ElementChangedEventArgs&lt;TextBoxEntry&gt; e)
          {
              base.OnElementChanged(e);
              if (e.OldElement != null &amp;&amp; Control != null) editText.FocusChange -= ControlOnFocusChange;
              if (e.NewElement != null)
              {
                  SetNativeControl(CreateNativeControl());
                  editText.ShowSoftInputOnFocus = false;
                  editText.FocusChange += ControlOnFocusChange;
                  editText.SetPadding(20, 0, 20, 0);
                  editText.SetTextSize(Android.Util.ComplexUnitType.Px, 32);
                  editText.TextChanged += (source, args) =&gt; { };
              }
              if (Control != null)
              {
                  GradientDrawable gd = new GradientDrawable();
                  gd.SetColor(Android.Graphics.Color.Transparent);
                  Control.SetBackground(gd);
                  editText.Background = null; // Remove underline.
                  var shape = new ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());
                  shape.Paint.Color = Xamarin.Forms.Color.Gray.ToAndroid();
                  shape.Paint.SetStyle(Paint.Style.Stroke);
                  Control.Background = shape;
              }
          }
          private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
          {
              if (args.HasFocus)
              {
                  editText.Post(() =&gt;
                  {
                      editText.RequestFocus();
                      if (mKeyboardView.Visibility == ViewStates.Gone) mKeyboardView.Visibility = ViewStates.Visible;
  
                      // Wanna resize scrollview, but couldn't get necessary info...!
                      var deviceDisplayInfo = DeviceDisplay.MainDisplayInfo;
                      var a_height = deviceDisplayInfo.Height;
                      var b_height = Element.ScrollView.Height;
                      var c_height = mKeyboardView.Height;
                  });
              }
              else
              {
                  // Hide keyboard
                  mKeyboardView.Visibility = ViewStates.Gone;
              }
          }
      }
  }
  
  </code></pre>
  <p>And lastly,
  Resources/xml/keyboard.xml:</p>
  <pre><code>&lt;Keyboard xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:keyWidth=&quot;25%p&quot; android:horizontalGap=&quot;0px&quot;
      android:verticalGap=&quot;0px&quot; android:keyHeight=&quot;27dip&quot;&gt;
  
    &lt;Row&gt;
      &lt;Key android:codes=&quot;8&quot;  android:keyLabel=&quot;1&quot; android:keyHeight=&quot;54dip&quot; android:keyEdgeFlags=&quot;left&quot; /&gt;
      &lt;Key android:codes=&quot;9&quot;  android:keyLabel=&quot;2&quot; android:keyHeight=&quot;54dip&quot; /&gt;
      &lt;Key android:codes=&quot;10&quot; android:keyLabel=&quot;3&quot; android:keyHeight=&quot;54dip&quot; /&gt;
      &lt;Key android:codes=&quot;81&quot; android:keyLabel=&quot;+&quot; android:keyHeight=&quot;81dip&quot; android:keyEdgeFlags=&quot;right&quot; /&gt;
    &lt;/Row&gt;
    &lt;Row&gt;&lt;/Row&gt;
    &lt;Row&gt;
      &lt;Key android:codes=&quot;11&quot; android:keyLabel=&quot;4&quot; android:keyHeight=&quot;54dip&quot; android:keyEdgeFlags=&quot;left&quot; /&gt;
      &lt;Key android:codes=&quot;12&quot; android:keyLabel=&quot;5&quot; android:keyHeight=&quot;54dip&quot; /&gt;
      &lt;Key android:codes=&quot;13&quot; android:keyLabel=&quot;6&quot; android:keyHeight=&quot;54dip&quot; /&gt;
    &lt;/Row&gt;
    &lt;Row&gt;
      &lt;Key android:codes=&quot;66&quot; android:keyLabel=&quot;RET&quot; android:keyHeight=&quot;81dip&quot; android:horizontalGap=&quot;75%p&quot; android:keyEdgeFlags=&quot;right&quot; /&gt;
    &lt;/Row&gt;
    &lt;Row&gt;
      &lt;Key android:codes=&quot;14&quot; android:keyLabel=&quot;7&quot; android:keyHeight=&quot;54dip&quot; android:keyEdgeFlags=&quot;left&quot; /&gt;
      &lt;Key android:codes=&quot;15&quot; android:keyLabel=&quot;8&quot; android:keyHeight=&quot;54dip&quot; /&gt;
      &lt;Key android:codes=&quot;16&quot; android:keyLabel=&quot;9&quot; android:keyHeight=&quot;54dip&quot; /&gt;
    &lt;/Row&gt;
    &lt;Row&gt;&lt;/Row&gt;
    &lt;Row&gt;
      &lt;Key android:codes=&quot;17&quot; android:keyLabel=&quot;*&quot; android:keyHeight=&quot;54dip&quot; android:keyEdgeFlags=&quot;left&quot; /&gt;
      &lt;Key android:codes=&quot;7&quot;  android:keyLabel=&quot;0&quot; android:keyHeight=&quot;54dip&quot; /&gt;
      &lt;Key android:codes=&quot;18&quot; android:keyLabel=&quot;#&quot; android:keyHeight=&quot;54dip&quot; /&gt;
      &lt;Key android:codes=&quot;67&quot; android:keyLabel=&quot;DEL&quot; android:keyHeight=&quot;54dip&quot; android:keyEdgeFlags=&quot;right&quot; /&gt;
    &lt;/Row&gt;
    &lt;Row&gt;&lt;/Row&gt;
  
  &lt;/Keyboard&gt;
  </code></pre>