To detect the language of the keyboard at the moment the EditText gains focus, you can use the following snippet
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = (EditText) findViewById(R.id.et);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String language = getKeyboardLanguage();
Log.d("language is", language);
}
}
});
}
private String getKeyboardLanguage() {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype inputMethodSubtype = inputMethodManager.getCurrentInputMethodSubtype();
return inputMethodSubtype.getLocale();
}
}
At first, my keyboard language was Dutch, and it printed
D/language is: nl
then, I changed my keyboard language to English/United Stated and it printed
D/language is: en_US
To detect whether the language is RTL or LTR, you could use this
private boolean isRTL(Locale locale) {
final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
}
This isRTL() method I found at https://stackoverflow.com/a/23203698/1843331
Call that with a Locale created from the String language
:
String language = getKeyboardLanguage();
boolean isRTL = isRTL(new Locale(language));