Text controls are likely to be the first type of control that you’ll work with in Android. Android has a complete, but not overwhelming, set of text controls. In this section, we are going to discuss the TextView, EditText, AutoCompleteTextView, and
MultiCompleteTextView controls. Figure 4–2 shows the controls in action.
TextView
The TextView control knows how to display text but does not allow editing. This might lead you to conclude that the control is essentially a dummy label. Not true. The TextView control has a few interesting properties that make it very handy. If you know that the content of the TextView is going to contain a web URL, for example, you can set the autoLink property to web and the control will find and highlight the URL. Moreover, when the user clicks the TextView, the system will take care of launching the browser with the URL.
Actually, a more interesting use of TextView comes via the android.text.util.Linkify class (see Listing 4–5).
Figure 4–2. Text controls in Android
Listing 4–5. Using the Linkify Class with a TextView
TextView tv =(TextView)this.findViewById(R.id.cctvex);
tv.setText("Please visit my website, http://www.sayedhashimi.com or email me at [email protected].");
Linkify.addLinks(tv, Linkify.ALL);
As shown, you can pass a TextView to the Linkify class to find and add links to the
content of the TextView. In our example, we call the addLinks() method of Linkify,
passing the TextView and a mask indicating what types of links that Linkify should look
for. Linkify can create links for text that looks like a phone number, an e-mail address,
a web URL, or a map address. Passing Linkify.ALL tells the class to “linkify” all of these
link types. Clicking a link will cause the default intent to be called for that action. For example, clicking a web URL will launch the browser with the URL. Clicking a phone
number will launch the phone dialer, and so on. The Linkify class can perform this work
right out of the box. You can also have the class linkify other content (such as a name) by giving it a regular expression along with the content-provider URI.
EditText
The EditText control is a subclass of TextView. As suggested by the name, the EditText
control allows for text editing. EditText is not as powerful as the text-editing controls
that you find in JFC, for example, but users of Android-based devices probably won’t type documents—they’ll type a couple paragraphs at most. Therefore, the class has
have the control correct common misspellings. You can use the capitalize property to have the control capitalize words, the beginning of sentences, and so on. You can set the phoneNumber property if you need to accept a phone number. You can also set the password property if you need a password field.
The default behavior of the EditText control is to display text on one line and expand as needed. In other words, if the user types past the first line, another line will appear, and so on. You can, however, force the user to a single line by setting the singleLine property to true. In this case, the user will have to continue typing on the same line. Software programming for mobile devices is all about helping the user make a decision quickly. Thus, a common task is to highlight or style a portion of the EditText’s content. You can do this statically or dynamically. Statically, you can apply markup directly to the strings in your string resources (<string name="styledText"><i>Static</i> style in an <b>EditText</b>.</string>) and then reference it in your XML or from code. Note that you can use only the following HTML tags with string resources: <i>, <b>, and <u>. Styling an EditText control’s content programmatically requires a little additional work but allows for much more flexibility (see Listing 4–6).
Listing 4–6. Applying Styles to the Content of an EditText Dynamically EditText et =(EditText)this.findViewById(R.id.cctvex5); et.setText("Styling the content of an editText dynamically"); Spannable spn = et.getText();
spn.setSpan(new BackgroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spn.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC) , 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
As shown in Listing 4–6, you can get the content of the EditText (as a Spannable object) and then set styles to portions of the text. The code in the listing sets the text styling to bold and italics and sets the background to red. You are not limited to bold, italics, and underline as before. You can use superscript, subscript, strikethrough and others.
AutoCompleteTextView
The AutoCompleteTextView control is a TextView with auto-complete functionality. In other words, as the user types in the TextView, the control can display suggestions for the user to select. Listing 4–7 demonstrates the AutoCompleteTextView control. Listing 4–7. Using an AutoCompleteTextView Control
AutoCompleteTextView actv = (AutoCompleteTextView) this.findViewById(R.id.ccactv); ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,
new String[] {"English", "Hebrew", "Hindi", "Spanish", "German", "Greek" }); actv.setAdapter(aa);
The AutoCompleteTextView control shown in Listing 4–7 suggests a language to the user.
For example, if the user types en, the control suggests English. If the user types gr, the control recommends Greek, and so on.
If you have used a suggestion control or a similar auto-complete control, then you know that controls like this have two parts: a text-view control and a control that displays the suggestion(s). That’s the general concept. To use a control like this, you have to create the control, create the list of suggestions, tell the control the list of suggestions, and possibly tell the control how to display the suggestions. Alternatively, you could create a second control for the suggestions and then associate the two controls.
Android has made this simple, as is evident from Listing 4–7. To use an
AutoCompleteTextView, you can define the control in your layout file and then reference it
in your activity. You then create an adapter class that holds the suggestions and define the ID of the control that will show the suggestion (in this case, a simple list item). In Listing 4–7, the second parameter to the ArrayAdapter tells the adapter to use a simple
list item to show the suggestion. The final step is to associate the adapter with the
AutoCompleteTextView, which you do using the setAdapter() method.
MultiAutoCompleteTextView
If you have played with the AutoCompleteTextView control, then you know that the
control offers suggestions only for the entire text in the text view. In other words, if you type a sentence, you don’t get suggestions for each word. That’s where
MultiAutoCompleteTextView comes in. You can use the MultiAutoCompleteTextView to
provide suggestions as the user types. For example, Figure 4–2 shows that the user typed the word English followed by a comma, and then Hi, at which point the control suggested Hindi. If the user were to continue, the control would offer additional suggestions.
Using the MultiAutoCompleteTextView is like using the AutoCompleteTextView. The
difference is that you have to tell the control where to start suggesting again. For example, in Figure 4–2, you can see that the control can offer suggestions at the beginning of the sentence and after it sees a comma. The MultiAutoCompleteTextView
control requires that you give it a tokenizer that can parse the sentence and tell it whether to start suggesting again. Listing 4–8 demonstrates using the
MultiAutoCompleteTextView control.
Listing 4–8. Using the MultiAutoCompleteTextView Control
MultiAutoCompleteTextView mactv = (MultiAutoCompleteTextView) this .findViewById(R.id.ccmactv);
ArrayAdapter<String> aa2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
new String[] {"English", "Hebrew", "Hindi", "Spanish", "German", "Greek" }); mactv.setAdapter(aa2);
The only significant difference between Listing 4–7 and Listing 4–8 is the use of
MultiAutoCompleteTextView and the call to the setTokenizer() method. Because of the CommaTokenizer in this case, after a comma (,) is typed into the EditText field, the field
will again make suggestions using the array of strings. Any other characters typed in will not trigger the field to make suggestions. So even if you were to type “French Spani” the partial word “Spani” would not trigger the suggestion because it did not follow a
comma.