In Android development, you can use LinkMovementMethod, Linkify, and AutoLink to make a TextView display HTML content and allow URLs to be clickable.
TextView Clickable Method 1:
XML Code
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="Visit https://www.google.com visit Google" />
Add HTML Formatting Text in TextView
TextView textView = findViewById(R.id.textView);
String text= "Click <a href='https://shafiq.thbd.in'>here</a> visit Portfolio.";
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));
textView.setMovementMethod(LinkMovementMethod.getInstance());
Converts HTML string into a formatted text. : Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT);
Enables link clicking: LinkMovementMethod.getInstance();
TextView Clickable Method 2:
If your text contains a plain URL (without HTML tags), then you can make it clickable in your TextView using Linkify.
String text= "Click https://shafiq.thbd.in visit Portfolio.";
TextView textView = findViewById(R.id.textView);
textView.setText(text);
Linkify.addLinks(textView, Linkify.WEB_URLS); textView.setMovementMethod(LinkMovementMethod.getInstance());
Detects and converts URLs into clickable links: Linkify.addLinks(textView, Linkify.WEB_URLS);
Note
Ensure that your link starts with http// or https// for proper functionality. also, if you have multiple types of links( e.g., web, email), you may need to handle them in different ways.
Linkify.addLinks(textView, Linkify.ALL);
TextView Clickable Method 3:
In the last part (I Recommend) if you prefer a simpler method, no need to do any code in Java or Kotlin then you can follow this
XML Code:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="Click https://shafiq.thbd.in visit Portfolio."
android:linksClickable="true" />
Automatically makes URLs clickable: autoLink=”web”
