SoFunction
Updated on 2025-04-06

Example of how to add hyperlinks to Android TextView

This article describes the method of adding hyperlinks to Android TextView. Share it for your reference, as follows:

public class Link extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    // text1 shows the android:autoLink property, which
    // automatically linkifies things like URLs and phone numbers
    // found in the text. No java code is needed to make this
    // work.
    // text2 has links specified by putting <a> tags in the string
    // resource. By default these links will appear but not
    // respond to user input. To make them active, you need to
    // call setMovementMethod() on the TextView object.
    TextView t2 = (TextView) findViewById(.text2);
    (());
    // text3 shows creating text with links from HTML in the Java
    // code, rather than from a string resource. Note that for a
    // fixed string, using a (localizable) resource as shown above
    // is usually a better way to go; this example is intended to
    // illustrate how you might display text that came from a
    // dynamic source (eg, the network).
    TextView t3 = (TextView) findViewById(.text3);
    (
      (
        "<b>text3:</b> Text with a " +
        "<a href=\"\">link</a> " +
        "created in the Java source code using HTML."));
    (());
    // text4 illustrates constructing a styled string containing a
    // link without using HTML at all. Again, for a fixed string
    // you should probably be using a string resource, not a
    // hardcoded value.
    SpannableString ss = new SpannableString(
      "text4: Click here to dial the phone.");
    (new StyleSpan(), 0, 6,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    (new URLSpan("tel:4155551212"), 13, 17,
          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    TextView t4 = (TextView) findViewById(.text4);
    (ss);
    (());
  }
}

For more information about Android related content, please check out the topic of this site:Android View View Tips Summary》、《Android layout layout tips summary》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.