0

I am trying to create another input method editor. I create and add the keyboard with the following code.

    keyboardView = (KeyboardView)findViewById(R.id.myKeboardView);

    Keyboard keyboard = new Keyboard(this, R.xml.key_layout, "abcdefghigjklmnopqrstuvwxyz", 10, 0);
    keyboardView.setKeyboard(keyboard);

The result is this. enter image description here

Upon checking the source code for the keyboard constructor, I saw that it is not adding new rows. The keyboard generated contains just one very long row and when the column limit is reached, instead of adding new row, it just increases the y value with the key height and continues off the same old row.

from the android source code for Keyboard class

 public Keyboard(Context context, int layoutTemplateResId, 
            CharSequence characters, int columns, int horizontalPadding) {
        this(context, layoutTemplateResId);
    int x = 0;
    int y = 0;
    int column = 0;
    mTotalWidth = 0;
    
    Row row = new Row(this);
    row.defaultHeight = mDefaultHeight;
    row.defaultWidth = mDefaultWidth;
    row.defaultHorizontalGap = mDefaultHorizontalGap;
    row.verticalGap = mDefaultVerticalGap;
    row.rowEdgeFlags = EDGE_TOP | EDGE_BOTTOM;
    final int maxColumns = columns == -1 ? Integer.MAX_VALUE : columns;
    for (int i = 0; i < characters.length(); i++) {
        char c = characters.charAt(i);
        if (column >= maxColumns 
                || x + mDefaultWidth + horizontalPadding > mDisplayWidth) {
            x = 0;
            y += mDefaultVerticalGap + mDefaultHeight;
            column = 0;
        }
        final Key key = new Key(row);
        key.x = x;
        key.y = y;
        key.label = String.valueOf(c);
        key.codes = new int[] { c };
        column++;
        x += key.width + key.gap;
        mKeys.add(key);
        row.mKeys.add(key);
        if (x > mTotalWidth) {
            mTotalWidth = x;
        }
    }
    mTotalHeight = y + mDefaultHeight;
    rows.add(row);
} 

Instead of just one row, i want as many rows as needed and the keys not squashed. Any work around for this issue?

Community
  • 1
  • 1
yohannist
  • 4,166
  • 3
  • 35
  • 58
  • I followed this tutorial for creating keyboard and it worked properly. Here is the link http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615 – Vivek Mishra Apr 05 '16 at 10:09
  • Yes, however that tutorial uses keys defined in xml to construct the keyboard. I'm talking about using Keyboard(Context context, int layoutTemplateResId, CharSequence characters, int columns, int horizontalPadding) to initialize the keyboard by supplying character sequence – yohannist Apr 05 '16 at 10:11
  • try passing context instead of this in new row – Vivek Mishra Apr 05 '16 at 10:14
  • what is value of default height and width? – Vivek Mishra Apr 05 '16 at 10:21
  • those come from com.android.internal.R.styleable.Keyboard_keyWidth and com.android.internal.R.styleable.Keyboard_keyHeight. – yohannist Apr 05 '16 at 10:23
  • debug their values it seems it is like wrap_content – Vivek Mishra Apr 05 '16 at 10:24
  • look at the constructor code i posted above. i think the fault is there. – yohannist Apr 05 '16 at 10:26
  • You can also try getting screen width and then dividing it by no of keys you want in 1 row and then use that value as default width – Vivek Mishra Apr 05 '16 at 10:28

0 Answers0