title: Using Custom XAML Controls in the Xamarin.Android Project with a Keyboard Service App
body: <p>I have a custom keyboard app I'm trying to write. I'd like to define the key layout as XAML control (KeyboardPage.xaml) in my shared code and then incorporate them into native code in the .Android and .iOS projects. Being primarily a desktop WPF developer, I'm comfortable with this approach.</p>
  
  <p>When my MainActivity inherits from FormsAppCompatActivity or AppCompatActivity, this is pretty straight forward. </p>
  
  <pre><code>namespace MobileTestApp2.Droid
  {
   [Activity(Label = "MobileTestApp2", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
   public class MainActivity : FormsAppCompatActivity
   {
    protected override void OnCreate(Bundle bundle)
    {
     base.OnCreate(savedInstanceState);
     Forms.Init(this, bundle);
     LoadApplication(new App());
    }
   }
  }
  </code></pre>
  
  <p>However, in order for the app to register as an Android keyboard service, MainActivity has to inherit from InputMethodService:</p>
  
  <pre><code>namespace TestKeyboard.Droid
  {
   [Service(Permission = "android.permission.BIND_INPUT_METHOD", Label = "Test Keyboard")]
   [MetaData("android.view.im", Resource = "@xml/method")]
   [IntentFilter(new string[] { "android.view.InputMethod" })]
   public class MainActivity : InputMethodService, KeyboardView.IOnKeyboardActionListener
   {
    private KeyboardView keyboardView;
    private Keyboard keyboard;
    private bool isCaps = false;
  
    public override View OnCreateInputView()
    {
     keyboardView = (KeyboardView)LayoutInflater.Inflate(Resource.Layout.Keyboard, null);
     keyboard = new Keyboard(this, Resource.Xml.Qwerty);
     keyboardView.Keyboard = keyboard;
     keyboardView.OnKeyboardActionListener = this;
  
     return keyboardView;
    }
  }
  </code></pre>
  
  <p>I'm not sure how to convert my XAML controls into Android controls; and how to get them to replace the Keyboard.axml and.or qwerty.xml layouts I have defined in the .Android project. </p>
  
  <p>From research, I found that this is possible using the CreateSupportFragment() method and the SupportFragmentManager member. However, the SupportFragmentManager member is only available when inheriting from FormsAppCompatActivity or AppCompatActivity. I tried instantiating a local object inheriting from FormsAppCompatActivity and replacing my Keyboard.axml and qwerty.xml layouts with my converted XAML control (KeyboardPage.xaml) before I instantiated a new Keyboard object; but it didn't work. I'm not even sure if the OnCreate() method is even being called. </p>
  
  <pre><code>namespace MurrayRegisterKeys.Droid
  {
   public class MainActivity : InputMethodService, KeyboardView.IOnKeyboardActionListener
   {
    private KeyboardView keyboardView = null;
    private Keyboard keyboard = null;
    private bool isQwerty = false;
  
    public override View OnCreateInputView()
    {
     new InnerActivity();
  
     KeyboardView keyboardView = (KeyboardView)LayoutInflater.Inflate(Resource.Layout.Keyboard, null);
     keyboard = new Keyboard(this, Resource.Xml.Keypad);
     keyboardView.Keyboard = keyboard;
     keyboardView.OnKeyboardActionListener = this;
     return keyboardView;
    }
   }
  
   public class InnerActivity : FormsAppCompatActivity
   {
    protected override void OnCreate(Bundle bundle)
    {
     base.OnCreate(bundle);
     Forms.Init(this, bundle);
     SetContentView(Resource.Layout.Keyboard);
     Android.Support.V4.App.Fragment keyboardPage = new KeyboardPage().CreateSupportFragment(this);
     SupportFragmentManager
         .BeginTransaction()
         .Replace(Resource.Xml.Qwerty, keyboardPage)
         .Commit();
    }
   }
  }
  </code></pre>
  
  <p>Any ideas? Is what I'm trying to do even possible? Are there alternate approaches for creating a new default keyboard service? </p>
  
  <p>Thanks!</p>