How To Create Clickable Links in A TextView (Android Studio)

TextView is a powerful functionality of android. We can show different text base materials like characters, numbers, strings. Here in this article, I will show you that how you can make links in TextView using multiple methods.

clickable links in a textView

Autolink Method

Using the autolink method you can make TextView clickable. You can open mail, phone dialer, web address, map using this method. If you are a beginner then this method is the easiest method. Because you don't need to touch Java or Kotlin for this. Here we are going to show all of them with example code.

Open web address using TextView

For this, you just need a TextView where you have to give the web address or link and identify it as a web address. In android:text you have to give the web link. For the web address, you should always use the domain part. For example, you have to add '.com,' '.org,' '.xyz' with the web address. Use android:autoLink to identify the text as a web address. Here is the example code for this:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Website: tech.pathgriho.com"
        android:autoLink="web" />

Open phone dialer using TextView

For this, you just need a TextView where you have to give the number and identify it as a phone number. In android:text you have to give the phone number. For phone numbers, you should always use the country code before the number. For example, you have to add +44 for the UK, +880 for Bangladesh, +91 for India. Use android:autoLink to identify the text as a phone number. Here is the example code for this:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone: +8800000000000"
        android:autoLink="phone" />

Open Gmail using TextView

For this, you just need a TextView where you have to give the mail address and identify it as a mail address. In android:text you have to give the mail address. Use android:autoLink to identify the text as a mail address. Here is the example code for this:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="E-mail: pathgriho71@gmail.com"
        android:autoLink="email" />

Use multi-type links in TextView

Using the autolink method you can easily show multiple link types in a single TextView. Create a string that contains multiple links and then connect them in the text view. Here we are sharing the code for you:

activity_main.xml

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/autolink_test"
        android:autoLink="all" />

strings.xml

<string name="autolink_test">Name: Techkib \n
   Email: pathgriho71@gmail.com \n
   Phone: +880000000000 \n
   Website: https://tech.pathgriho.com
</string>

Here we are using the all link type to support multiple types of links. Customize the full section with different materials for a better look.

WebView Method

You can also do it using WebView. Create an HTML file where you can link different sources. Give the text view an id and use Java or Kotlin code to send the user to another activity (Intent). In that activity show the HTML file in webview. This method is a little bit hard for beginners but if you have enough idea about webview and HTML then you can easily do it.

Read More: How To Display Toast in Android (Android Studio)

Conclusion

This is helpful for every entry-level app. Because in this way you don't need so many components. In the application developer section, you can use this method to show all information about your team. Check our other articles about the android studio. Subscribe to our email list for all new updates and free ebooks every month.
Post a Comment (0)
Previous Post Next Post