Text View

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).

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 contentprovider URI.

0 0

Post a comment