Add Hyperlink in android application through textview

Create HyperLink text in android apps <a href=””> to open given clickable link in mobile Web Browser.

Hyper Linking is the most usable method for all website because with the use of hyperlink website developer can insert links on their web pages and on each & every link holds a unique web address. Now with the use of spanned variable android developers can also insert hyperlinks in android apps using textview tag. So here is the complete step by step tutorial for Add Hyperlink in android application through textview.

android-project-download-code-button

How to Add Hyperlink in android application through textview.

Code for MainActivity.java file.

 package com.textviewhyperlink_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;


public class MainActivity extends Activity {

 TextView HyperLink;
 Spanned Text;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 HyperLink = (TextView)findViewById(R.id.textView1);
 
 Text = Html.fromHtml("Click on this link to visit my Website <br />" +
 "<a href='https://www.android-examples.com//'>Android-Examples.com</a>");

 HyperLink.setMovementMethod(LinkMovementMethod.getInstance());
 HyperLink.setText(Text);
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.textviewhyperlink_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Your Added Link Shows Here"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Screenshots:

Add Hyperlink in android application through textview

open link

Click Here To Download Add Hyperlink in android application through textview project.

Leave a Reply

Your email address will not be published. Required fields are marked *