6

I am writing an application which is using dictionaries. I want to switch between dictionaries any time user change input language. For example if the typing language is english work with english dictionary, if it is german, work with german dictionary. Is there any way to set a listener to get that change? All the answer i found were about locale and not about input language. I don't want to handle locale, locale has no effect in application, the input language does. (I am developing in minSDK=7)

Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
Christos Asa
  • 155
  • 1
  • 1
  • 13

3 Answers3

15
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();

String locale = ims.getLocale();

You can try this code to get Current Keyboard Language regional code.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Ramkailash
  • 252
  • 3
  • 5
  • 1
    doesn't work on samsung s5 API 23... reports en_US regardless of keyboard language selected. Seems like it's querying system settings and not the actual keyboard config at runtime. – Nerdy Bunz Jun 11 '18 at 07:08
  • 1
    return empty string – Alexei Oct 07 '19 at 15:56
  • yeah, same thing as @nerdy-bunz said: this is just the system language. How do you see which keyboard layout is selected? the app randomly will assign a layout when you plug it and I need to know it it's en_US or my language – Alberto M Sep 03 '21 at 13:42
  • Google keyboard, Kika keyboard from Xiami does not return language code.. what to do about that? any solution? – axita.savani Mar 02 '22 at 12:40
2

It is possible

imm.getCurrentInputMethodSubtype();

to return you null value. In this case you should check language of android system like this

Locale.getDefault().getLanguage()        ---> en     

otherwise you will receive NullPointerException

Stoycho Andreev
  • 6,163
  • 1
  • 25
  • 27
0
private fun printInputLanguages() {
    val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
    val ims = imm?.enabledInputMethodList
    if (ims != null) {
        for (method in ims) {
            val submethods = imm.getEnabledInputMethodSubtypeList(method, true)
            for (submethod in submethods) {
                if (submethod.mode == "keyboard") {
                    val currentLocale = submethod.locale
                    Timber.e("Available input method locale: $currentLocale")
                }
            }
        }
    }
}
Vanya Rachel
  • 1,329
  • 1
  • 18
  • 20