4

I am creating my own IME for device. What I need to add is a TextBox above keyboardView as shown in below image. Although I am able to display it as shown in below image but I am unable to write text into it.

enter image description here

I am extending keyboard view and below is the layout structure

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/wrapper" android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="fill_parent" android:background="@color/background" > <TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_height="wrap_content" android:id="@+id/txtTest" android:layout_width="fill_parent" android:text="Test" ></TextView> <EditText android:inputType="text" android:id="@+id/edtTest" android:layout_height="wrap_content" android:layout_width="fill_parent"></EditText> <com.keyboard.CustomKeyboardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/keyboard" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:keyTextSize="15sp" /> </LinearLayout> 

public class CustomKeyboardView extends KeyboardView {

static final int KEYCODE_OPTIONS = -100; private TextView mResultText; public CustomKeyboardView (Context context, AttributeSet attrs) { super(context, attrs); } public CustomKeyboardView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected boolean onLongPress(Key key) { if (key.codes[0] == Keyboard.KEYCODE_CANCEL) { getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS, null); return true; } else { return super.onLongPress(key); } } 

Thanks, Nil

5
  • Could you provide at least your layout? Are you extending KeyboardView? Commented Oct 23, 2011 at 11:07
  • @Laurent updated with code do I need to add anything else to the layout? Commented Oct 24, 2011 at 6:20
  • I think this is your Activity layout, not your CustomKeyboardView layout. Could you provide the CustomKeyboardView layout (or onCreate()) ? Commented Oct 24, 2011 at 7:36
  • In which textbox do you want to enter text? The EditText or the TextView? Is this layout your application's or is this the layout of your inputmethodservice? Commented Oct 24, 2011 at 16:41
  • @Laurent the layout above I have added is the input.xml and in OnCreateInputView we are loading this layout. And there is no code added to CustomKeyboardView still edited to just to clarify it. Commented Oct 24, 2011 at 16:46

1 Answer 1

2

You can capture your softkeyboard events and transmit them to your own widgets by implementing KeyboardView.OnKeyboardActionListener.

In your Inputmethodservice.onKey() Method, you should try to transmit the event to your InputView subviews like this:

public class mySoftKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener { // Implementation of KeyboardViewListener inside your InputMethodService public void onKey(int primaryCode, int[] keyCodes) { //assuming your inputview is in private variable mInputView //and contains public members txtTst and edtTst views //(arrange for this in your InputView.onCreate) //Here, we just transmit the onKey code to View.onKeyDown/Up and let views draw themselves sendKey( mInputView.txtTst , primaryCode ); // send this to your TextView sendKey( mInputView.edtTst , primaryCode ); // also send to your EditText } /** * Helper to send a character to the editor as raw key events. */ private void sendKey(View v, int keyCode) { v.onKeyDown(keyCode,new KeyEvent(KeyEvent.ACTION_DOWN, keyCode)); v.onKeyUp (keyCode,new KeyEvent(KeyEvent.ACTION_UP, keyCode)); } //other interface function, no need to implement public void onText(CharSequence text){} public void swipeRight() {} public void swipeLeft() {} public void swipeDown() {} public void swipeUp() {} public void onPress(int primaryCode) {} public void onRelease(int primaryCode) {} } 

Edit

To answer your comment about the difference between glyph and keycode, here's a code snippet that can help you:

//This snippet tries to translate the glyph 'primaryCode' into key events //retrieve the keycharacter map from a keyEvent (build yourself a default event if needed) KeyCharacterMap myMap=KeyCharacterMap.load(event.getDeviceId()); //event list to reproduce glyph KeyEvent evs[]=null; //put the primariCode into an array char chars[]=new char[1]; chars[0]=primaryCode; // retrieve the key events that could have produced this glyph evs=myMap.getEvents(chars); if (evs != null){ // we can reproduce this glyph with this key event array for (int i=0; i< evs.length;i++) mySendKeyMethodHelper(evs[i]); } else { /* could not find a way to reproduce this glyph */ } 
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Laurent thanks for your reply,I tried overriding the onKey method but its still not working. I am loading the Custom Layout as given above along with CustomKeyboardView. So suppose we click on the 'To' field of Messaging application, we see that keyboard getting launched with our layout (i.e with EditText shown above), but no matter what we do all the text that we enter using the Keyboard (which is my custom IME) goes to the'To' field of Messaging application and not to EditText that is currently displayed. It seems the IME is binded with 'To' field and we are not able to change it.
It is bounded to the field which has focus (the To field in your case). Per design. You still can get key events in onKey an defer them to your own textviews.
I forgot to mention that you must implement OnKeyboardActionListener to get a chance to capture keys. See the updated code.
Thank you Laurent you are a life saver... finally did it with your suggestions. The only thing is it is not showing the exact characters. But will manage it, must do something with primarycode or keycodes, maybe related with the unicode or like that...
@nilMoBile I just added a snippet I coded in my softkeyboard to handle this kind of issues. The trick is to get a KeyCharacterMap and use it to translate unicode characters into keyEvents list. (To send characters to a textview one needs to get an InputConnection which I found only available when the editext has focus)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.