How to Add Hyperlink to TextView in Android?
Hello geeks, today we are going to make an application where we will see that how we can add a link to a TextView in Android, and using this concept we will add portals – home and practice (of GeeksforGeeks) in our application. So that, user can directly go to these portals from our application.
What we are going to build in this article?
We will be using the strings.xml file to do our task and then will us setMovementMethod() in our java file so that whatever link we provided we can direct there. Here is how our application will look like.
So, now let us see step by step implementation of the application.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working on strings.xml file
Open values > strings.xml file. We have to make two strings to add a hyperlink, so use the code provided below.
Step 3: Working with the activity_main.xml file
In the activity_main.xml file, we have to name the TextViews by a string that we created in the strings.xml file. Below is the code for it.
After implementing the above code our UI looks like this.
Step 4: Working with the MainActivity.java file
We will be using setMovementMethod() to redirect users to the provided link. We will also be using setcolorlink() method to change the color of the link according to our convenience. Below is the code for it.
Congratulations, you have successfully made a complete application its time to run and test it. You can use this method to redirect users of your application to your website. Here is the final output of your application.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.