Android: How/where to put gesture code into IME?
- by CardinalFIB
Hi, I'm new to Android but I'm trying to create an IME that allows for gesture-character recognition. I can already do simple apps that perform gesture recognition but am not sure where to hook in the gesture views/obj with an IME. Here is a starting skeleton of what I have for the IME so far. I would like to use android.gesture.Gesture/Prediction/GestureOverlayView/OnGesturePerformedListener. Does anyone have advice?
--
CardinalFIB
gestureIME.java
public class gestureIME extends InputMethodService {
private static Keyboard keyboard;
private static KeyboardView kView;
private int lastDisplayWidth;
@Override public void onCreate() {
super.onCreate();
}
@Override public void onInitializeInterface() {
int displayWidth;
if (keyboard != null) {
displayWidth = getMaxWidth();
if (displayWidth == lastDisplayWidth) return;
else lastDisplayWidth = getMaxWidth();
}
keyboard = new GestureKeyboard(this, R.xml.keyboard);
}
@Override public View onCreateInputView() {
kView = (KeyboardView) getLayoutInflater().inflate(R.layout.input, null);
kView.setOnKeyboardActionListener(kListener);
kView.setKeyboard(keyboard);
return kView;
}
@Override public View onCreateCandidatesView() {
return null;
}
@Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
super.onStartInputView(attribute, restarting);
kView.setKeyboard(keyboard);
kView.closing(); //what does this do???
}
@Override public void onStartInput(EditorInfo attribute, boolean restarting) {
super.onStartInput(attribute, restarting);
}
@Override public void onFinishInput() {
super.onFinishInput();
}
public KeyboardView.OnKeyboardActionListener kListener = new KeyboardView.OnKeyboardActionListener() {
@Override public void onKey(int keyCode, int[] otherKeyCodes) {
if(keyCode==Keyboard.KEYCODE_CANCEL) handleClose();
if(keyCode==10) getCurrentInputConnection().commitText(String.valueOf((char) keyCode), 1); //keyCode RETURN
}
@Override public void onPress(int primaryCode) {} // TODO Auto-generated method stub
@Override public void onRelease(int primaryCode) {} // TODO Auto-generated method stub
@Override public void onText(CharSequence text) {} // TODO Auto-generated method stub
@Override public void swipeDown() {} // TODO Auto-generated method stub
@Override public void swipeLeft() {} // TODO Auto-generated method stub
@Override public void swipeRight() {} // TODO Auto-generated method stub
@Override public void swipeUp() {} // TODO Auto-generated method stub
};
private void handleClose() {
requestHideSelf(0);
kView.closing();
}
}
GestureKeyboard.java
package com.android.jt.gestureIME;
import android.content.Context;
import android.inputmethodservice.Keyboard;
public class GestureKeyboard extends Keyboard {
public GestureKeyboard(Context context, int xmlLayoutResId) {
super(context, xmlLayoutResId);
}
}
GesureKeyboardView.java
package com.android.jt.gestureIME;
import android.content.Context;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.Keyboard.Key;
import android.util.AttributeSet;
public class GestureKeyboardView extends KeyboardView {
public GestureKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GestureKeyboardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override protected boolean onLongPress(Key key) {
return super.onLongPress(key);
}
}
keyboard.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="@dimen/key_height"
>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="-3" android:keyLabel="Close" android:keyWidth="20%p" android:keyEdgeFlags="left"/>
<Key android:codes="10" android:keyLabel="Return" android:keyWidth="20%p" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>
input.xml
<?xml version="1.0" encoding="utf-8"?>
<com.android.jt.gestureIME.GestureKeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gkeyboard"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>