How to load a different keyboard layout for a subtype in android?

I am using a sample android keyboard . it includes En (US) and En (GB) subtypes. When you select any of subtypesit, it only changes the flag on a space.

Let's say I want to change the layout based on what is selected subtype, but I cannot do this.

So far I have created another xml file for the English language (GB), and I call it qwerty_gb.xml(for testing, I changed the keys Returnand Del)

Then declared closed

LatinKeyboard mQwertyKeyboardGB;

and initialized it along with the default keyboards in the onInitializeInterfaceoverrid method for Softkeyboard.java

So:

mQwertyKeyboardGB = new LatinKeyboard(this, R.xml.qwerty_gb);

I do not know what I am missing here.

+4
1

SoftKeyboard.java , .

1- .

mQwertyKeyboard = new LatinKeyboard(this, R.xml.qwerty);
mPersianKeyboard = new LatinKeyboard(this, R.xml.persian);

2- OnCreateInputView , .

InputMethodSubtype subtype = mInputMethodManager.getCurrentInputMethodSubtype();
switch(subtype.getLocale()) {
    case "fa_IR":
        setLatinKeyboard(mPersianKeyboard);
        break;
    case "en_US":
        setLatinKeyboard(mQwertyKeyboard);
        break;
}

mPersianKeyboard, fa_IR. fa_IR method.xml.

<subtype
    android:label="@string/label_subtype_generic"
    android:icon="@drawable/icon_en_us"
    android:imeSubtypeLocale="en_US"
    android:imeSubtypeMode="keyboard" />
<subtype
    android:label="@string/label_subtype_generic"
    android:icon="@drawable/icon_en_gb"
    android:imeSubtypeLocale="fa_IR"
    android:imeSubtypeMode="keyboard" />

3- , , onCurrentInputMethodSubtypeChanged :

@Override
public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) {
    mInputView.setSubtypeOnSpaceKey(subtype);
    switch(subtype.getLocale()) {
        case "fa_IR":
            setLatinKeyboard(mSymbolsKeyboard);
            break;
        case "en_US":
            setLatinKeyboard(mQwertyKeyboard);
            break;
    };
}

. getLocale() API 24. getLanguageTag(). .

+3

Source: https://habr.com/ru/post/1609506/


All Articles