The SoftKeyboard sample is actually structured similar to what you need, although you do not know it. Let's break it - this is a complete stub to act as a service of an input method and use a standard XML keyboard:
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.Keyboard; public class MyInputMethod extends InputMethodService { private Keyboard mKeyboard; @Override public void onInitializeInterface() { mKeyboard = new Keyboard(this, R.xml.qwerty); } @Override public View onCreateInputView() { mInputView = (KeyboardView) getLayoutInflater().inflate( R.layout.input, null); mInputView.setKeyboard(mKeyboard); return mInputView; } }
Note that two XML documents are named. The first, res/xml/qwerty.xml , defines the layout, so the KeyboardView class knows how to draw a keyboard.
But the layout he is inflating, res/layout/input.xml , consists of this (simplified):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"> <android.inputmethodservice.KeyboardView android:id="@+id/keyboard" /> </LinearLayout>
That's all you need to declaratively create a view! Creating a standalone View not much different than creating an Activity . You do not have a standard activity life cycle, but both environments provide you with access to XML layouts. All you have to do is use the blower, refer to any subheadings you need, and then return the main view.
So, I hope you can inflate your layout by thinking about it.
You donβt even need to use XML layouts if your layout is simple enough. If your input method can consist entirely of one view, you can simply create it directly in onCreateInputView . Heres a complete stub input method that does not use any XML:
public class MyInputMethod extends InputMethodService { MyView view; @Override public View onCreateInputView() { return view = new MyView(this); } }
(Of course, the templates in the manifest and the res/xml/method.xml will still be there.)