14

Problem Description

I'm trying to detect current selected language of the keyboard. For that purpose I use following code:

Code

/* Return the handle to a system-level service by name. The class of the returned
 * object varies by the requested name. */
 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
/* Returns the current input method subtype. This subtype is one of the subtypes in the
 * current input method. This method returns null when the current input method doesn't
 * have any input method subtype. */
 InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
/* The locale of the subtype. This method returns the "locale" string parameter passed
 * to the constructor. */
 String locale = ims.getLocale();

but application throes NoSuchMethodError exception on getCurrentInputMethodSubtype function.

Question

Is there any way to get Keyboard selected language? If no how I can detect the language in which user typing in the application?

Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • What's the `minSDK` in the `AndroidManifest`? – g00dy Jul 16 '13 at 07:54
  • @g00dy `minSDK` is 8 I know for this method I need 11 :( – Viktor Apoyan Jul 16 '13 at 07:56
  • Yes, you can access that, unless the `minSDK` is 11. To determine the language of the phone use `useLocale.getDefault().getDisplayLanguage();`. – g00dy Jul 16 '13 at 07:57
  • @g00dy I want to detect not the language of the phone, I want to know in which language user type, for example if he type in english I will show message if in Russian I will do some other action. This is what I want. – Viktor Apoyan Jul 16 '13 at 08:00
  • I'vo no idea how you can do that and if it's even possible. For the Roman languages - you have even common words. The russian language that you mentionned is cyrillic, but so is the Bulgarian too (they both have common word) - how can you distinguist between them? – g00dy Jul 16 '13 at 08:03
  • @g00dy actually I use English, Russian and Armenian in my app, so I must think how I can do that :) – Viktor Apoyan Jul 16 '13 at 08:05
  • 1
    then why not let the user choose that before typing. A Stting maybe, which states - which language to you prefer ընկեր. And then, based on that do your thing after the user types the text in the box :) – g00dy Jul 16 '13 at 08:08
  • Google keyboard, Kika keyboard from Xiami does not return language code.. what to do about that? any solution? – axita.savani Mar 03 '22 at 08:33

5 Answers5

8

Here's what I did to get the available input languages:

private void printInputLanguages() {
   InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
   List<InputMethodInfo> ims = imm.getEnabledInputMethodList();

   for (InputMethodInfo method : ims) {
       List<InputMethodSubtype> submethods = imm.getEnabledInputMethodSubtypeList(method, true);
       for (InputMethodSubtype submethod : submethods) {
          if (submethod.getMode().equals("keyboard")) {
             String currentLocale = submethod.getLocale();
             Log.i(TAG, "Available input method locale: " + currentLocale);
          }
       }
   }
}
erakitin
  • 11,437
  • 5
  • 44
  • 49
diman
  • 429
  • 3
  • 15
5

You can get keyboard language by first detecting the locale of device keyboard and then getting the Locale object from it. For example,

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
    String localeString = ims.getLocale();
    Locale locale = new Locale(localeString);
    String currentLanguage = locale.getDisplayLanguage();

Here, currentLanguage will give your keyboard language. Hope this helps.

AndyN
  • 1,742
  • 1
  • 15
  • 30
  • But be aware that it can return "zz" locale for example if QWERTY keyboard selected. – Fedir Tsapana Nov 01 '16 at 12:10
  • 1
    `ims.getLocale()` return empty string. Also it is deprecated from API 24 onwards – Bugs Happen Mar 06 '19 at 07:45
  • `getLocale()` is only deprecated because there's a newer, better `getLanguageTag()`. so android developers still believe in this functionality. the fact remains tho that not all keyboards will give you this information. – j__m Aug 14 '21 at 12:58
  • 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
3

1st Method

To get the selected language of your device. This might help u

Locale.getDefault().getLanguage()        ---> en     
Locale.getDefault().getISO3Language()    ---> eng
Locale.getDefault().getCountry()         ---> US
Locale.getDefault().getISO3Country()     ---> USA
Locale.getDefault().toString()           ---> en_US
Locale.getDefault().getDisplayLanguage() ---> English
Locale.getDefault().getDisplayCountry()  ---> United States
Locale.getDefault().getDisplayName()     ---> English (United States)

2nd Method

You can extract the language from the current locale. You can extract the locale via the standard Java API, or by using the Android Context. For instance, the two lines below are equivalent:

String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0

No. There's a phone locale, but the keyboard may ignore it by design (many keyboards allow you to switch languages just within the keyboard for bilingual users). There's no API for the keyboard to report it has done so to the OS.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • so there is no way to do that? – Viktor Apoyan Jul 16 '13 at 07:57
  • Nope. The OS doesn't have the concept of a keyboard language. Its done completely by the keyboards, and there's no way to query them. You might find a keyboard that does it, but its not a common feature. – Gabe Sechan Jul 16 '13 at 07:59
-3

If you want to get the selected language of your device. This might help u

Locale.getDefault().getDisplayLanguage();

You might also do:

Locale.getDefault().toString() which gives a string that fully identifies the locale, e.g. "en_US".

Ankit Aggarwal
  • 2,367
  • 24
  • 30