1

I'm trying to implement a keyboard with a TextView that updates with each keypress, but it won't show up on the screen. It should appear above the top row of letters.

When I run the keyboard, the keys show up with an extra row on top but nothing inside

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:id="@+id/keyboardView"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="60dp"
    >
    <Row>
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:text="@string/text_view_default"
            android:visibility="visible"/>
            <!--android:layout_height="50dp"-->
    </Row>

    <Row>
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3"/>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
        <Key android:codes="55" android:keyLabel="7"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9"/>
        <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
    </Row>
    .
    .
    .
</Keyboard>

2 Answers2

1

You will have to change your keyboard layout to contain your TextView and KeyboardView.

1) change IME layout as below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <TextView
            android:id="@+id/tvKeyboard"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center"
            android:padding="10dp"
            android:scaleType="fitEnd"
            android:src="@drawable/keyboard_icon" />

    </RelativeLayout>

    <android.inputmethodservice.KeyboardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/keyboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:keyPreviewLayout ="@layout/preview"
    />

</LinearLayout>

2) In your IME class make a few changes:

@Override 
public View onCreateInputView() {

    final View root = getLayoutInflater().inflate(R.layout.idee_keyboard_layout, null);

    TextView tvKeyboard = (TextView) root.findViewById(R.id.tvRevertKeyboard);

    tvKeyboard.setText("set your text here");

    kv = (KeyboardView) root.findViewById(R.id.keyboard);

    keyboard = new Keyboard(this, R.xml.qwerty);
    kv.setKeyboard(keyboard);
    kv.setOnKeyboardActionListener(this);
    return root;
} 

Done.

Prasad Pawar
  • 1,606
  • 1
  • 15
  • 30
  • The new program is now giving me an error: The specified child already has a parent. You must call removeView() on the child's parent first. – Joseph Buchoff Dec 23 '15 at 02:31
  • I checked another StackOverflow for this problem and changed the view inflator line to:final View root = getLayoutInflater().inflate(R.layout.idee_keyboard_layout, null, false); – Joseph Buchoff Dec 23 '15 at 02:32
  • Nvm I got it working! I commented out the lines that had to do with defining or adding views one-by-one and somehow it worked without changing anything. Thanks Prasad! – Joseph Buchoff Dec 23 '15 at 02:58
  • The error you are talking about occurs when you are adding a view to some layout but that view was already added to some other layout before. So, whenever you are done with such layouts, call `layout.removeAllViews()` immediately. Anyways you've fixed it. Good job! – Prasad Pawar Dec 23 '15 at 09:20
0

Nesting the TextView in the ReleativeLayout and that RelativeLayout with the KeyboardView in a LinearLayout resulted in the TextView being displayed. A couple more bugs came up, but after rebuilding the project, they subsided. Thanks for the help!