Android – Detect string/user input language

android

I wanted to know if there is a way to detect if the user's input is in greek charset.

Edit:
Just to be more clear, I need to recognize the language the user types and not the phone's locale.

For example, my phone is in English and let's say my keyboard is in Russian, the getDefault() returns "en", but I need to have "ru" at that point.

I do not know if this is available out of the box from android, maybe an approach to detect the string's character codes and see if is in English alphabet or in another. Any points on this?

I imagine something like
if character belongs to K then is English
(where K is the essemble of english characters)

Solution:

Finally I used regular expression to determine if the string is in English.

String pattern = "^[A-Za-z0-9. ]+$";
if (string.matches(pattern) 
   // is English
else
   // is not English

If someone has to propose a better solution I will mark it as answer.

Best Answer

You can use following method instead of pattern matching:

boolean isEnglish = true;
for ( char c : s.toCharArray() ) {
  if ( Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN ) {
    isEnglish = false;
    break;
  }
}
Related Topic