I am recently making my first Android App and it has a Edittext
area which plans to only allow users to input correctly spelled words. Basically I have already learned how to use layout properties such as Android:inputType
to detect any misspelled words. Any misspelled words should be marked with a red underline. But I cannot find a way to prevent users from inputting misspelled words.
The ideal situation is: if a user has input any misspelled words and clicks the submit button, a prompt message (for example a Toast message) would appear to inform the user to modify misspelled words before they can really submit.
Follow steps from this link to create a spelling checker.
http://www.tutorialspoint.com/android/android_spelling_checker.htm
Then modify the sample code above to meet your requirement:
E.g. When (arg0.length == 0), that means there is no suggestion (no spelling mistake), you can create validation from here.
However, it could be a word that is not written in English. So you would need a language detection:
https://code.google.com/p/language-detection/
(From: How to detect language of user entered text?)
What you have to do to achieve this is implement spellchecksession listener.
May be you can use spell check listener along with a text watcher.
SpellCheckListener
You can use this method to validate word(Spell check).
public boolean CheckForWord(String Word){
try {
BufferedReader in = new BufferedReader(new FileReader("/usr/share/dict/american-english"));
String str;
while ((str = in.readLine()) != null) {
if ( str.indexOf( Word) != -1 ) {
return true;
}
}
in.close();
}
catch (IOException e) {
}
return false;
}
And on SUBMIT Button Click
btnSUBMIT.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String EdittextValue = edittext.getText().toString();
if(CheckForWord(EdittextValue)){
Toast.makeText(getActivity(),
"Correct Word " + EdittextValue ,
Toast.LENGTH_LONG).show();
// Do something here.
}
else{
Toast.makeText(getActivity(),
"Wrong Word " + EdittextValue ,
Toast.LENGTH_LONG).show();
}
}
});