I'm trying to detect current selected language of the keyboard. For that purpose I use following 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.
Is there any way to get Keyboard selected language? If no how I can detect the language in which user typing in the application?
You can also get to the page from your Android device's main settings menu. From the settings: Select “System.” Select “Languages & input” > “On-screen keyboard” > “Gboard” > “Languages.”
On a hardware keyboard, press and hold the Windows logo key , and then press the Spacebar to cycle through your input methods. If you have a touchscreen, you can switch your touch keyboard layout by tapping or clicking the keyboard icon, and then tapping or clicking the keyboard layout you want to switch to.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With