InputFilter objects enforce such rules as allowing only uppercase text and

Inputfilter objects enforce such rules as allowing

This preview shows page 26 - 31 out of 99 pages.

InputFilter objects enforce such rules as allowing only uppercase text and limiting the length of the text entered You can create custom filters by implementing the InputFilter interface, which contains the single method called filter() .
Image of page 26

Subscribe to view the full document.

Constraining User Input with Input Filters (Cont’d) final EditText text_filtered = (EditText) findViewById(R.id.input_filtered); text_filtered.setFilters(new InputFilter[] { new InputFilter.AllCaps(), new InputFilter.LengthFilter(2) }); Here is an example of an EditText control with two built-in filters that might be appropriate for a two-letter state abbreviation. The setFilters() method call takes an array of InputFilter objects. This is useful for combining multiple filters, as shown. In this case, we convert all input to uppercase. Additionally, we set the maximum length to two characters long. The EditText control looks the same as any other, but if you try to type in lowercase, the text is converted to uppercase, and the string is limited to two characters. This does not mean that all possible inputs are valid, but it does help users to not concern themselves with making the input too long or bother with the case of the input. This also helps your application by guaranteeing that any text from this input is a length of two characters. It does not constrain the input to only letters, though.
Image of page 27
Helping the User with Autocompletion The Android SDK also provides a way to help the user with entering commonly used data into forms. This functionality is provided through the autocomplete feature. There are two forms of autocomplete: One is the more standard style of filling in the entire text entry based on what the user types. If the user begins typing a string that matches a word in a developer- provided list, the user can choose to complete the word with a tap. This is done through the AutoCompleteTextView control. The second method allows the user to enter a list of items, each of which has autocomplete functionality. These items must be separated in some way by providing a Tokenizer to the MultiAutoCompleteTextView object. A common Tokenizer implementation is provided for comma- separated lists using the MultiAutoCompleteTextView.CommaTokenizer object.
Image of page 28

Subscribe to view the full document.