Dynamically auto-capitalize, auto-correct, or password field in an EditText

Go To StackoverFlow.com

2

I would like to programmatically enable or disable auto-capitalization, auto-correction or password-field (showing bullets) in an EditText. This means NOT from XML.

I would also like to avoid TextWatcher solutions, and more focus on InputFilter or some other solution.

Manipulating the EditText as an Editable allows the attachment of InputFilters, however I was unable to get these to work programmatically. Also, EditText methods such as setAllCaps did nothing in practice for me. This is true for auto-correction as well. This is my attempted auto-correction (to show you where I am at, and some of my thought process):

/** SpellCheck filter for auto-correcting words. */
class SpellCheckFilter implements InputFilter {

    public String word;

    public SpellCheckFilter()
    {
        word = " ";
    }

    //FIXME not returning corrected word. Try adjusting start/end values,
    //what range does this return?
    @Override
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {
        word += source;
        Log.i("SpellCheckFilter", "source=\"" + source + "\";  word=\"" + word + "\"");
        if (source.toString().endsWith(" "))
        {
            word = word.replace(" ", "");
            String correction = AutoText.get(word, 0, word.length()-1, view);
            Log.i("TextEditor", "Corrected word=" + (correction == null ? word : correction));
            word = " ";
            return correction;
        }
        return null;
    }

}

Using InputFilter.AllCaps, I was able to get an almost-working auto-capitalization method, however the first letter did not auto-capitalize.

2012-04-04 04:54
by Phil
could whoever down-voted please explain why?? - Phil 2012-04-04 04:55
Wasn't my downvote. You would improve your question, however, if you detailed exactly what you tried (include code) - Michael Petrotta 2012-04-04 04:57


0

    //Calling Method
    setListener1(editText);
    //Method
    public void setListener1(final AppCompatEditText edittext) {
            final TextWatcher textWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {}
                @Override
                public void afterTextChanged(Editable s) {
                    String input = StringStaticMethods.firstCapital(s.toString());
                    edittext.removeTextChangedListener(this);
                    edittext.setText("");
                    edittext.append(input);
                    edittext.addTextChangedListener(this);
                }
            };
            edittext.addTextChangedListener(textWatcher);
        }
public static String firstCapital(String str) {
        if (str != null && !str.equals("")) {
            return (str.substring(0, 1).toUpperCase() + str.substring(1, str.length()));
        }
        return "";
    }
    //Use in XML
    android:inputType="textFilter|textMultiLine|textNoSuggestions"
2019-01-01 11:03
by Piyush Mathur
Ads