Automatic phone number and URL links in Android-Text

Source: Internet
Author: User

If the textview text contains a phone number or a website address, you can call or open the webpage by clicking the phone number or website address. In Android, you have provided us with such attributes and methods for setting, there are three types:

1. Set the autolink attribute of textview: it has the following values: All, web, phone, and email. When there are these types of text values in the text, click it to enter the webpage, call, or e-mail activity. This is the simplest method.

2. Add links directly to text values

(1) For example, in string. in the XML file: <string> <a href = http://www.google.com> http://www.google.com </a> <a href = "Tel: 18600000001"> Tel </a> </string>, set the textview property setmovementmethod (linkmovementmethod. getinstance ());

(2) Use hteml. fromhtml to build text in the code

Code

         tv2.setText(
Html.fromHtml("the google url: " +
"<a href=\"http://www.google.com\">http://www.google.com</a><br/>" +
"the telephone: " +
"<a href=\"tel:18603045201\">18603045201</a>)"
));
tv2.setMovementMethod(LinkMovementMethod.getInstance());

3. Use spanablestring to specify a string as the link text

 

Code

         TextView tv3=(TextView)findViewById(R.id.tv3);
SpannableString ss=
new SpannableString("the google url: http://www.google.com 18600000001");
ss.setSpan(new URLSpan("http://www.google.com"),
16, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:18603045201"),
38, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv3.setText(ss);
tv3.setMovementMethod(LinkMovementMethod.getInstance());

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.