{"id":2019,"date":"2020-02-09T21:19:11","date_gmt":"2020-02-09T11:19:11","guid":{"rendered":"https:\/\/learntodroid.com\/?p=2019"},"modified":"2021-01-13T09:54:56","modified_gmt":"2021-01-12T23:54:56","slug":"how-to-create-a-hyperlink-using-android-textview","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/how-to-create-a-hyperlink-using-android-textview\/","title":{"rendered":"How to Create a Hyperlink Using Android TextView"},"content":{"rendered":"\n

The TextView is a widget in Android that allows you to display text to the user. It is also possible to use the Android TextView widget to create a hyperlink which can load a web page in a mobile web browser when clicked.<\/p>\n\n\n\n

To create a clickable hyperlink in your Android app using the TextView widget follow the steps below.<\/p>\n\n\n\n

  1. Create a new String resource in the strings.xml<\/strong> resource file representing the text to be placed inside the TextView<\/li>
  2. In your String resource use HTML markup to insert the hyperlink using an anchor tag<\/strong><\/li>
  3. Add a TextView in the layout of your Activity or Fragment and use the android:text<\/strong> attribute on the TextView to reference the String resource containing the hyperlink<\/li>
  4. In your Activity or Fragment class use the setMovementMethod(…) <\/strong>on the TextView to enable the hyperlink<\/li><\/ol>\n\n\n\n

    In the article below I will include Java and Kotlin code samples that show how to create a hyperlink using the Android TextView following the steps listed above.<\/p>\n\n\n\n

    I have also included some additional instructions along with code samples written in Kotlin for using links in Android apps in different ways such as:<\/p>\n\n\n\n

    • How to Change the Hyperlink Text Color in a TextView<\/li>
    • How to Create a Clickable Link to a Website Using a Button in Android<\/li>
    • How to Create a Clickable Link to Another Activity in a TextView<\/li><\/ul>\n\n\n\n

      Android TextView Hyperlink Tutorial<\/h2>\n\n\n\n

      Below is a screenshot of an Android app we will be building in this tutorial for creating a hyperlink using the Android TextView widget. When the hyperlink is selected the app will open a mobile web browser and navigate the user to the website http:\/\/10.0.0.14:32769<\/a><\/p>\n\n\n\n

      \"\"<\/figure>\n\n\n\n

      In this tutorial will cover the following steps for creating a hyperlink using the Android TextView widget.<\/p>\n\n\n\n

      1. Creating a String Resource for the Hyperlink in HTML Markup<\/li>
      2. Creating an Activity Layout File with a TextView<\/li>
      3. Enabling the TextView Hyperlink in the Activity or Fragment Class<\/li><\/ol>\n\n\n\n

        I will include code excerpts embedded into this tutorial. For step 3, I have included code samples for both Kotlin and Java.<\/p>\n\n\n\n

        I have created a video tutorial embedded below on how to create a hyperlink inside a TextView in an Android app. The code written in this video tutorial is also available on GitHub<\/a>.<\/p>\n\n\n\n

        \n
        https:\/\/youtu.be\/tL9fCKC2lNA<\/a>