Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 428
Phil Avatar asked Mar 19 '12 13:03

Phil


People also ask

How do I set input type programmatically?

This example demonstrates how to set the input type for an Android EditText programmatically using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is input type in EditText android?

EditText in Android UI or applications is an input field, that is used to give input text to the application. This input text can be of multiple types like text (Characters), number (Integers), etc.

What is the difference between an EditText and a TextView?

EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.

What is EMS in EditText?

The em is simply the font size. In an element with a 2in font, 1em thus means 2in. Expressing sizes, such as margins and paddings, in em means they are related to the font size, and if the user has a big font (e.g., on a big screen) or a small font (e.g., on a handheld device), the sizes will be in proportion.


2 Answers

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.

like image 82
f1vefour Avatar answered Sep 29 '22 11:09

f1vefour


I hope you've found an answer to the question. The answer might help those those come to the thread later. So, you can set multiple tags in similar manner as you do in XML using a | (pipe). Something like:

EditText mEditText = new EditText(this);
mEditText.setInputType(InputTpe.TYPE_TEXT_FLAG_CAP_CHARACTERS|InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);

Also, depending on your situation you might want to use setInputTypeor setRawInputype.

like image 22
Shobhit Puri Avatar answered Sep 29 '22 11:09

Shobhit Puri