The Android SDK provides a number of controls for retrieving data from users. One of the most common types of data that applications often need to collect from users is text. Two frequently used controls to handle this type of job are EditText controls and Spinner controls.
Clickable AutoLinks: URL launches browser, phone number launches dialer, and street address launches Google Maps.
Retrieving Text Input Using EditText Controls
The Android SDK provides a convenient control called EditText to handle text input from a user. The EditText class is derived from TextView. In fact, most of its functionality is contained within TextView but enabled when created as an EditText.The EditText object has a number of useful features enabled by default, many of which are shown in Figure.
First, though, let’s see how to define an EditText control in an XML layout file:
This layout code shows a basic EditText element. There are a couple of interesting things to note. First, the hint attribute puts some text in the edit box that goes away when the user starts entering text. Essentially, this gives a hint to the user as to what should go there. Next is the lines attribute, which defines how many lines tall the input box is. If this is not set, the entry field grows as the user enters text. However, setting a size allows the user to scroll within a fixed sized to edit the text. This also applies to the width of the entry.
By default, the user can perform a long press to bring up a context menu. This provides to the user some basic copy, cut, and paste operations as well as the ability to change the input method and add a word to the user’s dictionary of frequently used words.You do not need to provide any additional code for this useful behavior to benefit your users. You can also highlight a portion of the text from code, too. A call to setSelection() does this, and a call to selectAll() highlights the entire text entry field.
Various styles of EditText controls and Spinner and Button controls.
The EditText object is essentially an editable TextView.This means that you can read text from it in the same way as you did with TextView: by using the getText() method. You can also set initial text to draw in the text entry area using the setText() method. This is useful when a user edits a form that already has data. Finally, you can set the editable attribute to false, so the user cannot edit the text in the field but can still copy text out of it using a long press.
Helping the User with Auto Completion
In addition to providing a basic text editor with the EditText control, the Android SDK also provides a way to help the user with entering commonly used data into forms. This functionality is provided through the auto-complete feature.
There are two forms of auto-complete. 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 just 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 MultiAutoComplete TextView object that handles this method. A common Tokenizer implementation is provided for comma-separated lists and is used by specifying the MultiAuto CompleteText View.CommaTokenizer object. This can be helpful for lists of specifying common tags and the like.
A long press on EditText controls typically launches a Context menu for Select, Cut, and Paste.
Using AutoCompleteTextView (left) and MultiAutoCompleteTextView (right).
Both of the auto-complete text editors use an adapter to get the list of text that they use to provide completions to the user. This example shows how to provide an AutoComplete TextView for the user that can help them type some of the basic colors from an array in the code:
In this example, when the user starts typing in the field, if he starts with one of the letters in the COLORS array, a drop-down list shows all the available completions. Note that this does not limit what the user can enter. The user is still free to enter any text (such as puce).The adapter controls the look of the drop-down list. In this case, we use a built-in layout made for such things. Here is the layout resource definition for this
There are a couple more things to notice here. First, you can choose when the completion drop-down list shows by filling in a value for the completionThreshold attribute. In this case, we set it to a single character, so it displays immediately if there is a match. The default value is two characters of typing before it displays auto-completion options. Second, you can set some text in the completionHint attribute. This displays at the bottom of the drop-down list to help users. Finally, the drop-down list for completions is sized to the TextView. This means that it should be wide enough to show the completions and the text for the completionHint attribute.
The MultiAutoCompleteTextView is essentially the same as the regular auto-complete, except that you must assign a Tokenizer to it so that the control knows where each autocompletion should begin. The following is an example that uses the same adapter as the previous example but includes a Tokenizer for a list of user color responses, each separated by a comma:
As you can see, the only change is setting the Tokenizer. Here we use the built-in comma Tokenizer provided by the Android SDK. In this case, whenever a user chooses a color from the list, the name of the color is completed, and a comma is automatically added so that the user can immediately start typing in the next color. As before, this does not limit what the user can enter. If the user enters “maroon” and places a comma after it, the auto-completion starts again as the user types another color, regardless of the fact that it didn’t help the user type in the color maroon. You can create your own Tokenizer by implementing the Multi Auto Complete Text View.Tokenizer interface. You can do this if you’d prefer entries separated by a semicolon or some other more complex separators.
Constraining User Input with Input Filters
There are often times when you don’t want the user to type just anything. Validating input after the user has entered something is one way to do this. However, a better way to avoid wasting the user’s time is to filter the input. The EditText control provides a way to set an InputFilter that does only this.
The Android SDK provides some InputFilter objects for use. There are InputFilter objects that 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(). 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 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. It does not constrain the input to only letters, though. Input filters can also be defined in XML.
Giving Users Input Choices Using Spinner Controls
Sometimes you want to limit the choices available for users to type. For instance, if users are going to enter the name of a state, you might as well limit them to only the valid states because this is a known set. Although you could do this by letting them type something and then blocking invalid entries, you can also provide similar functionality with a Spinner control. As with the auto-complete method, the possible choices for a spinner can come from an Adapter. You can also set the available choices in the layout definition by using the entries attribute with an array resource (specifically a string-array that is referenced as something such as @array/state-list).The Spinner control isn’t actually an EditText, although it is frequently used in a similar fashion. Here is an example of the XML layout definition for a Spinner control for choosing a color:
This places a Spinner control on the screen.When the user selects it, a pop-up shows the prompt text followed by a list of the possible choices. This list allows only a single item to be selected at a time, and when one is selected, the pop-up goes away.
There are a couple of things to notice here. First, the entries attribute is set to the values that shows by assigning it to an array resource, referred to here as @array/colors.
Filtering choices with a spinner control.
Second, the prompt attribute is defined to a string resource. Unlike some other string attributes, this one is required to be a string resource. The prompt displays when the popup comes up and can be used to tell the user what kinds of values that can be selected from.
Because the Spinner control is not a TextView, but a list of TextView objects, you can’t directly request the selected text from it. Instead, you have to retrieve the selected View and extract the text directly:
As it turns out, you can request the currently selected View object, which happens to be a TextView in this case. This enables us to retrieve the text and use it directly. Alternatively, we could have called the getSelectedItem() or getSelectedItemId() methods to deal with other forms of selection.
Setting Up Your Android Development Environment
Writing Your First Android Application
Understanding The Anatomy Of An Android Application
Defining Your Application Using The Android Manifest File
Managing Application Resources
Exploring User Interface Screen Elements
Designing User Interfaces With Layouts
Drawing And Working With Animation
Using Android Data And Storage Apis
Sharing Data Between Applications With Content Providers
Using Android Networking Apis
Using Android Web Apis
Using Location-based Services (lbs) Apis
Using Android Multimedia Apis
Using Android Telephony Apis
Using Android 3d Graphics With Opengl Es
Using The Android Ndk
Using Android’s Optional Hardware Apis
Working With Notifications
Working With Services
Extending Android Application Reach
Managing User Accounts And Synchronizing User Data
Handling Advanced User Input
Targeting Different Device Configurations And Languages
The Mobile Software Development Process
Designing And Developing Bulletproof Android Applications
Testing Android Applications
Selling Your Android Application
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.