Search code examples
javaandroidautocorrect

How can I Create an Autocorrect in Java that Will Let Me Replace User Input as They Type?


I am trying to alter user input in real time in an android app (I'm using android studio). When the user is typing and has a spelling error, I want to create my own autocorrect that will automatically correct the error for them.

For example, the user intends to type "Meeting with Rob." By accident, they type "Meetng"

Without the user having to click a suggestion or anything, "Meetng" becomes "Meeting" just as the regular autocorrect on your phone would do.

Note:

Code efficiency doesn't matter in this case and I don't want to use the regular built in autocorrect because this part of the code is part of something else that requires me to have full control over the autocorrection happening. I don't think I can use the regular autocorrect but please correct me if I am wrong.

Code:

void checkWordAndAutocorrectIt(){

            if (dict.contains(VEvent.DESCRIPTION) || newDictionary.contains(VEvent.DESCRIPTION)) {
                //then you don't need to do anything I don't think but
                //this is here in case it starts ignoring words that haven't been autocorrected
                //so I can append them manually onto the string
            } else if (!dict.contains(VEvent.DESCRIPTION) && !newDictionary.contains(VEvent.DESCRIPTION)) {
                VEvent.DESCRIPTION.replace(VEvent.DESCRIPTION, autoCorrector(VEvent.DESCRIPTION));
                //this should hopefully replace the incorrect word with the corrected one returned by autoCorrector
                for (int i = 1; i <= 3; i++) {
                    if (val.get(i).autoCorrection == "") {
                        val.get(i).autoCorrection = autoCorrector(VEvent.DESCRIPTION);
                    } //this should check if there is a word already in the spot
                    //if not, it should save the autocorrected word.
                }
            }
        }

Thank you for any help, suggestions or resources you can provide!


Solution

  • So, according to this (How to convert input char to uppercase automatically in Java), apparently changing user input as it is typed is not possible in java, which is what I feared. Guess I'm going to be changing my project.