How do I set multiple input types in an EditText on Android?

Phil picture Phil · Mar 19, 2012 · Viewed 19.4k times · Source

I am trying to create an EditText with auto-capitalization and auto-correction implemented. I have manually figured out how to add InputFilters to allow auto-capitalization, though this only works after the first letter is typed, and I have had no luck with auto correction (I tried to create an InputFilter that used AutoText, but I'm not sure how all that works). Ideally, I could just use EditText.setInputType(...) to handle everything, but so far this has not worked. Is there a way to achieve this? My failed attempt is shown below (I just get normal input).

EditText mEditText = new EditText(this);
int inputType = InputType.TYPE_CLASS_TEXT;
if (auto_capitalize) {
    inputType = mEditText.getInputType() | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
}
if (auto_correct) {
    inputType = mEditText.getInputType() | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
}
mEditText.setInputType(inputType);

Please note, I am only interested in solutions for creating this EditText in code - not via XML.

Edit

I found sound new documentation describing TextKeyListener, however after trying to use this:

mEditText.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, true));

and using @farble1670's idea of using setRawInputType, so as not to affect the KeyListeners, there is still no change to the text.

Answer

f1vefour picture f1vefour · Apr 18, 2013

Through XML it would be setup like so.

android:inputType="textMultiLine|textNoSuggestions"

You simply add a pipe (|) between variables. I see you were doing it through code but I was just throwing this out there for reference.