Set a Link Into TextView

Description:
This example will let you set a link in textview will will switch to your phone’s browser and will open the link specified in browser. Also you will be able to dial a number by clicking a link set in the view.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it TextViewLinkDemo.

2.) Write following code into your main.xml

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:text="@string/link_text_auto"
/>

android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

android:id="@+id/text4"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

 

 

3.) Write following code into strings.xml

 

Hello World, TextViewLinkDemoActivity!
TextViewLinkDemo

<b>text1:</b> This is some text. In
this text are some things that are actionable. For instance,
you can click on http://www.gmail.com and it will launch the
web browser. You can click on google.com too. And, if you
click on (415) 555-1212 it should dial the phone.

 

 

4.) Build and run your code and check the output given below in the doc.

Steps:

1.) Create a project named TextViewLinkDemo and set the information as stated in the image.

Build Target: Android 2.3
Application Name: TextViewLinkDemo
Package Name: com.org. TextViewLinkDemo
Activity Name: TextViewLinkDemo
Min SDK Version: 9

2.) Open TextViewLinkDemo.java file and write following code there:

 

package com.org.TextViewLinkDemo;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;

public class TextViewLinkDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(
Html.fromHtml(
"<b>text3:</b> Text with a " +
"<a href="http://www.gmail.com">link</a> " +
"created in the Java source code using HTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());

SpannableString ss = new SpannableString(
"text4: Click here to dial the phone.");

ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
}

}

 

 

3.) Compile and build the project.

4.) Run on simulator and click onto the links shown in the view. It will open browser and browse the link you have clicked in text view.

Output

Leave a Comment:

13 comments
deepa says September 19, 2011

Thanks for this!!!!really nice topic

Reply
shanisk says September 20, 2011

[…] Set a link into TextView Posted by Sushant on August 30, 2011 Description: This example will let you set a link in textview will will switch to your phone’s browser and will open the link specified in browser. Also you will be able to dial a number by clicking a link set in the view. Algorithm: 1.) Create a new project by File-> New -> Android Project name it… Filed in: Android Development 1 […]

Reply
Sourav Mukherjee says October 22, 2011

Thanks…..nice tutorial..

Reply
MrBrownie says December 30, 2011

how does he now that when you click on “here” that he must call?
I can’t find it in the code?! help me please!

Reply
MrBrownie says December 30, 2011

do you have a solution to remove the underline?

Reply
upendar arvaplli says February 11, 2012

very nice

Reply
mangesh says March 28, 2012

can we open new activity class.

Reply
ohara says November 20, 2012

how to open new activity class with link? help me

Reply
vivek says December 7, 2012

great boss!!!!!!!!!!!!!!!

Reply
Ankit says December 26, 2012

can we open new activity class using link..

Reply
Jerome says March 4, 2013

hi,
i am jerome. i have a one doubt in sapannable concept. how to use sapanable in intent concept.

Reply
web page says October 2, 2013

I have read so many articles or reviews concerning the blogger lovers except this paragraph is
in fact a nice piece of writing, keep it up.

Reply
Android Textview - Cars And Autos Blog says October 19, 2014

[…] Set a link into textview | android programming tutorials […]

Reply
Add Your Reply