Android custom keyboard layout leaving a margin of white at the side

空扰寡人 提交于 2019-12-04 01:42:25
Yvette Colomb

I cannot reproduce the error, I have provided a screenshot, with a light background app and all my relevant code. I am not sure exactly where you are going wrong without seeing the whole project and directories, so I've posted the relevant xml. There is a service class also, I haven't included (as you obviously have it working).

The <Keyboard> code is in a file called qwerty.xml in the resources/xml folder, with a file called method.xml:

I used all your keyboard dimensions in the qwerty.xml, except I replaced the image.

method.xml

<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android">
<subtype
    android:label="@string/subtype_en_US"
    android:imeSubtypeLocale="en_US"
    android:imeSubtypeMode="keyboard" />
</input-method>

Two layout files:

keyboard.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
/>

preview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00"
    android:gravity="center"
    android:textSize="30sp
    android:textStyle="bold">
</TextView>

Styles:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

In the manifest, within application:

<service
    android:name=".SimpleIME"
    android:label="@string/simple_ime"
    android:permission="android.permission.BIND_INPUT_METHOD"
    >
    <meta-data
        android:name="android.view.im"
        android:resource="@xml/method"/>
    <intent-filter>
        <action android:name="android.view.InputMethod"/>
    </intent-filter>
</service>

I've left the image so large so you can clearly see there is no gap.

This link here also steps through this code:

http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

edit 1

After discussion in the comments, I would suggest checking your background style as there are issues/changes with the default background color may be showing for the keyboard window. This is addressed in detail issue in this answer https://stackoverflow.com/a/33052648/3956566. I won't reiterate the details, as the link is unlikely to rot, being an upvoted SO post.

edit 2

Ok Adding the difference in the input method service:

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();

    switch(primaryCode){
        case Keyboard.KEYCODE_DELETE :
            ic.deleteSurroundingText(1, 0);
            break;
        case Keyboard.KEYCODE_SHIFT:
            caps = !caps;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            break;
        case Keyboard.KEYCODE_DONE:
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
            break;
        default:
            char code = (char)primaryCode;
            if(Character.isLetter(code) && caps){
                code = Character.toUpperCase(code);
            }
            ic.commitText(String.valueOf(code),1);
    }
}

edit 3

Create a blank project, no activity for your keyboard, to test and run it as a separate app. You are using android:theme="@style/AppTheme.NoActionBar" In your launcher activity and there may be an issue with background opacity and your windows/views.

In you case: 7 (keys) x 14% = 98% + 1% gap (firstkeyś horizontalGap) = 99%.

Put the '%1' in horizontalGap in last key as you did in first key.

<Row>
        <Key android:codes="49" 
             android:keyIcon="@drawable/rsz_emoji" 
             android:horizontalGap="1%p" 
             android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyIcon="@drawable/rsz_emoji"/>
        <Key android:codes="51" android:keyIcon="@drawable/rsz_emoji"/>
        <Key android:codes="52" android:keyIcon="@drawable/rsz_emoji"/>
        <Key android:codes="53" android:keyIcon="@drawable/rsz_emoji"/>
        <Key android:codes="54" android:keyIcon="@drawable/rsz_emoji"/>
        <Key android:codes="48" 
             android:keyIcon="@drawable/rsz_emoji" 
             android:horizontalGap="1%p"
             android:keyEdgeFlags="right"/>
</Row>
sayan

Have you tried adding this to the keyboard tag in your xml file?

android:layout_height="fill_parent"
android:layout_width="fill_parent"

add a row with 100 percent width and zero heigh before first row. like this. add this code before first row

<Row android:horizontalGap="0%p" android:keyHeight="0.01%p">
        <Key android:codes="32" android:keyLabel=""
            android:keyWidth="100%p"
            />

    </Row>
trans

I had this problem too. I never found a good solution.

Android's Keyboard layout API sucks. If I were doing this all over again I would not have used it and instead created my own UI from scratch interfacing with InputMethod directly.

In any case, my solution was to add an extra key at the bottom of the layout with a 100% width and only 1dp height.

The button does absolutely nothing and the height is so small the user doesn't see it. But the width of the key fixes the gap issue.

If the keyboardview is in a layout, make sure the padding parameters are set to 0dp.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!