Now we will learn to embed Hyperlinks in a TextView.
First make a text view in the layout file.
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/link1" />
Now open the "strings.xml" file under the "values" folder. And add the following code to it.
<string name="link1"><![CDATA[This is Link to <a href="http://www.google.com/">Google</a>]]></string>
Finally make changes in the activity file.
package com.codingredefined.linkinatextview;
import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml(getString(R.string.link1)));
Linkify.addLinks(tv, Linkify.ALL);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
The following output will be obtained.
Clicking on Google will automatically open google.com in your default browser.













