1

My application is a kind of quiz: the user should type the words into a text field (EditText) correctly, user can type it in either language, depending on what the language he has selected (using the "spacebar swipe") on the soft keyboard. The soft keyboard is shown just before and hidden just after the user's input. But when the soft keyboard appears next time, it's input language is switched to system's default, do not remains the same, which user had used the last time.

Detailed

I have a EditText field:

<EditText
    android:id="@+id/textInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textNoSuggestions|textVisiblePassword"
/>

those input types are selected to avoid IME to show words suggestions. (Somehow textNoSuggestions only is not enough. But it is not the question)

When the user need to enter the word, I force the soft keyboard to appears like this:

void showSoftKeyboard() {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(mTextInput, InputMethodManager.SHOW_FORCED);
}

After the user has finished input, I'm forcing the keyboard to disappear (to no longer occupy the screen), using this method:

void hideSoftKeyboard() {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mTextInput.getWindowToken(), 0);
}

Problem

When the soft keyboard appears the next time, the language is system's default. How can I avoid this?

  • Can I somehow force the soft keyboard to restore it's last state?
  • Or may be it is possible to determine the chosen language when keyboard is hiding, and restore it, when the keyboard to be shown?
  • Or can you suggest any others solutions?

What I tried to do

I tried to use switchToLastInputMethod to force the keyboard to restore it's state:

void showSoftKeyboard() {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(mTextInput, InputMethodManager.SHOW_FORCED);
    final IBinder token = mTextInput.getWindowToken();
    if (token != null) imm.switchToLastInputMethod(token);
}

futile. The language is still being switched to the system default.

Note: the input fields are not recreated between inputs. They are still the same, only hiding.

Also, I tried to remember InputMethodSubtype, and force it to restore:

InputMethodSubtype mInputMethodSubtype;

void hideSoftKeyboard() {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    mInputMethodSubtype = imm.getCurrentInputMethodSubtype();
    imm.hideSoftInputFromWindow(mTextInput.getWindowToken(), 0);
}

void showSoftKeyboard() {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(mTextInput, InputMethodManager.SHOW_FORCED);
    if (mInputMethodSubtype != null) {
        imm.setCurrentInputMethodSubtype(mInputMethodSubtype);
    }
}

but this requires the WRITE_SECURE_SETTINGS permission, and that is allowed only for system apps. So, it is not applicable.

Thank you

AterLux
  • 4,566
  • 2
  • 10
  • 13
  • Well, I don't think you can change it unless it is a System App. You can read [this](http://stackoverflow.com/a/36639133/3921977) answer and also read about creating custom keyboards. – Aishwarya Tiwari Apr 15 '17 at 09:22
  • Thank you. I had read that before, but I hope there is some more native method. In fact, I not need to change the input language, I just want it to remain unchanged between inputs – AterLux Apr 15 '17 at 09:29
  • check [this](http://gunhansancar.com/change-language-programmatically-in-android/) and [this](http://stackoverflow.com/questions/38569922/change-keyboard-language-programatically). – Mehran Zamani Apr 15 '17 at 11:46

0 Answers0