Create Clickable Link on TextView
Note: This has been posted on my old blog 9 years ago. I decided to rewrite it here again as a way to preserve and refreshed using Kotlin
Technique explained in this post usually used in the login/register screen. Sometime as a way to guide users to “Terms and Conditions” of your app or to go to login or register screen. But it can be applied to a whole lot of different scenarios.
Fill The TextView
We will fill our TextView
with string generated using HTML
syntax.
Set the TextView
to be clickable and assign LinkMovementMethod
as the MovementMethod
value. These two line will allow us to click the link.
Under The Hood Explanation
As you can see, out register link above is set to launch.ltactivity://OpenPage?page=register
. This method uses android internal intent communication’s mechanism. By having launch.ltactivity://
, we tell android to call whatever activity that listen to it. Since we want our app to do it, we’ll have to catch using intent filter that specified inside manifest.xml
:
After this, we just need to read the data sent to LTActivity
. For example, for the above URL, when clicked, will go to LTActivity and using the following syntax:
will produce:
Btw, you should read this documentation from Mozilla about “What is a URL?” for more information.