0

Any one have idea of getting language of user, who is typing in the EditText.

What I Have Tried ?

  1. Please do not suggest Google's com.google.mlkit , I have already tried but not working when user types fast.
  2. I have also tried setting up android:digits="All Alfabets", It is not working when I long press and paste from the ClipBoard, It is allowing text from the other language.
ADM
  • 20,406
  • 11
  • 52
  • 83
Kishan Mevada
  • 662
  • 1
  • 6
  • 17
  • I don't think its possible detecting keyboard language .. if you just wants to restrict your edit text to alphabets `android:digits` should work or you can also try adding a custom InoutFilter to Edit text . – ADM Jul 29 '22 at 10:07
  • would you like to suggest a code for the custom filters for the EditText @ADM – Kishan Mevada Jul 29 '22 at 10:17
  • For your alphabets case `android:digits` should work fine . can you add the code with question with `android:digits` which is not working for you .. It might be device dependent so test in some other device as well . for custom InputFilter you can check [This thread](https://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext-in-android) – ADM Jul 29 '22 at 12:11

1 Answers1

0

This seems like a very complex problem! And one that limiting the allowed characters won't solve - many non-English languages use the same Latin character set as English, or use it for a romanised version of their written language. nihongo o kaite imasu is Japanese, but that would pass an alphabet check!

Even where other characters are used (e.g. accented versions) it's not unusual for people to just drop them and use the "standard English" characters when typing, especially if they're being informal - e.g. Spanish uses accents on question words like ¿qué?, but people might just not bother (and skip the ¿ too, or just say k if they're being really informal)

And then there's the fact that English does use accented characters - someone can be naïve or blasé, but you don't want your app to tell people they're "not typing in English" if they write those things.


I don't know anything about mlkit but if it's capable of detecting language to some decent degree, it really might be the way to go for such a complex human problem. I'd suggest that instead of trying to interfere with the user's typing, you just trigger a check when they're done which validates what they've entered. If it looks ok, you can enable a button or whatever - if not, show an error message and make them fix it themselves.

You could do that kind of thing with a TextWatcher (or the doAfterTextChanged extension function that comes with the ktx-core AndroidX library) - you'd probably want to start a delayed task so it happens a moment after they stop typing, and that you can interrupt if they start typing again

val languageCheck = Runnable {
    // do your check here, enable buttons / show errors as a result
}

// set up the checker
textView.doAfterTextChanged {
    // cancel an existing delayed task
    textView.removeCallbacks(languageCheck)
    // schedule a new one
    textView.postDelayed(languageCheck, delayMillis)
}
cactustictacs
  • 17,935
  • 2
  • 14
  • 25