Im trying to create an IME for android that is not a traditional keyboard (rows of keys corresponding to the different letters) and I am having trouble finding helpful resources
The SoftKeyboard sample is in fact structured similarly to what you need, though you don’t know it. Let’s break it down — here’s a complete stub to act as an input method service and use the 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 that the KeyboardView
class knows how to draw the keyboard.
But the layout which it inflates, 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>
This is all you need to declaratively create a view! Creating a stand-alone View
isn’t much different from creating an Activity
. You don’t have the standard activity lifecycle, but both environments give you access to XML layouts. All you need to do is use the inflater, reference any subviews you need, and then return the main view.
So hopefully you’ll be able to inflate your layout after thinking about this.
You don’t even need to use XML layouts if your layout is simple enough. If your input method can consist entirely of a single view, you can just instantiate it directly in onCreateInputView
. Here’s a complete stub input method service that doesn’t use any XML:
public class MyInputMethod extends InputMethodService {
MyView view;
@Override
public View onCreateInputView() {
return view = new MyView(this);
}
}
(Of course the boilerplate in the manifest and res/xml/method.xml
files would still be there.)