I am designing one android application in English and Chinese both. I want to know whether the user type English text or Chinese text?. Is there any way to check this in android?
-
use a TextWatcher for your EditText – Ameer Jul 10 '14 at 09:30
-
2Take each char from the input and see if the unicode point matches the range for chinese characters (http://stackoverflow.com/questions/1366068/whats-the-complete-range-for-chinese-characters-in-unicode) – Suau Jul 10 '14 at 09:32
4 Answers
If you want to detect whether the input string contains Chinese-like character(s) (CJK), the following may help you:
public static boolean isCJK(String str){
int length = str.length();
for (int i = 0; i < length; i++){
char ch = str.charAt(i);
Character.UnicodeBlock block = Character.UnicodeBlock.of(ch);
if (Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(block)||
Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS.equals(block)||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A.equals(block)){
return true;
}
}
return false;
}

- 2,086
- 1
- 20
- 33
-
Can you please tell me, how to detect if its in the English language? what code should I change? – Isuru Bandara Sep 29 '20 at 14:01
The accepted answer is either incomplete or outdated. Here are a few methods you can use to test if a character is a CJK Ideograph. My fuller answer is here.
It is better to use the codepoint rather than charAt
(as in the accepted answer) because many Chinese characters are in a higher code plane. Using charAt
will just give you one of the surrogate pairs rather than the actual Chinese character. So a better way to loop through a String is like this:
final int length = myString.length();
for (int offset = 0; offset < length; ) {
final int codepoint = Character.codePointAt(myString, offset);
// use codepoint here
offset += Character.charCount(codepoint);
}
And testing the codepoints can be done in one of the following ways.
private boolean isCJK(int codepoint) {
Character.UnicodeBlock block = Character.UnicodeBlock.of(codepoint);
return (Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(block)||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A.equals(block) ||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B.equals(block) ||
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C.equals(block) || // api 19, remove these if supporting lower versions
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D.equals(block) || // api 19
Character.UnicodeBlock.CJK_COMPATIBILITY.equals(block) ||
Character.UnicodeBlock.CJK_COMPATIBILITY_FORMS.equals(block) ||
Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS.equals(block) ||
Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT.equals(block) ||
Character.UnicodeBlock.CJK_RADICALS_SUPPLEMENT.equals(block) ||
Character.UnicodeBlock.CJK_STROKES.equals(block) || // api 19
Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION.equals(block) ||
Character.UnicodeBlock.ENCLOSED_CJK_LETTERS_AND_MONTHS.equals(block) ||
Character.UnicodeBlock.ENCLOSED_IDEOGRAPHIC_SUPPLEMENT.equals(block) || // api 19
Character.UnicodeBlock.KANGXI_RADICALS.equals(block) ||
Character.UnicodeBlock.IDEOGRAPHIC_DESCRIPTION_CHARACTERS.equals(block));
}
Or for API 19
private boolean isCJK(int codepoint) {
return Character.isIdeographic(codepoint);
}
Or for API 24
private boolean isCJK(int codepoint) {
return (Character.UnicodeScript.of(codepoint) == Character.UnicodeScript.HAN);
}
If you want to get default language of device Locale.getDisplayLanguage() should give you user's language.
Otherwise, this may help you.
EDIT:
I am not sure but Google Translate does this. When user types something, it automatically detects the language. So, Google Translate API should be able to do this for you.
EDIT2
Yes, it does with a simple HttpGet, here is the link.

- 13,560
- 9
- 60
- 109
-
Actually my company has it own keyboard in English and Chinese. Even if device language is changed, user can use both languages. So this wont work for me – Dr. Yatish Bathla Jul 10 '14 at 09:36
-
If you are using your own keyboard, shouldn't it be easy for you to detect which keyboard is selected? – Seshu Vinay Jul 10 '14 at 09:38
-
Actually Detecting Keyboard is not the Issue.I have created an Greeting application where user store 10 messages. Some are english and some are chinese.Depending chinese message is pressed or English one, i want to display different Output.That why i want to recognize the language – Dr. Yatish Bathla Jul 10 '14 at 10:08
-
1While saving those 10 messages itself, you could add a flag which you can use later to know which language it is. – Seshu Vinay Jul 10 '14 at 10:27
String language = Locale.getDefault().getDisplayLanguage(); will give your default language of your device
System.out.println("My locale::"+Locale.getDefault().getDisplayLanguage());
it will print like My locale:: english;
now you have to check
if(language.equalignorecase("engish")){
// do your stuff for english
}else{
// do your stuff for chienese
}
also you can use Language Detection Library for android

- 10,224
- 2
- 37
- 59
-
This is to detect language used in android. I want to know specific language type in my activity. For example: Text1.IsThisEnglishOrChinese()..Something like this – Dr. Yatish Bathla Jul 10 '14 at 10:13