TextView with link in ANDROID…….

By | June 24, 2012

Hi all…….

All of you may be familiar with TextViews in ANDROID.
But how many of you know that we have have html as text in a textView. This post is a simple example to show this.

Create a fresh project and copy this code to the main java file.

package com.coderzheaven;

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

public class TextViewLinkDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText( Html.fromHtml("<b>This is a textView with a link </b>  " +
                    " <br /> <a href="http://www.coderzheaven.com">Coderzheaven</a> " +
                    "created in the Java source code using HTML."));
         tv.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

The main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
	android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

The AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TextViewLinkDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Link in TextView Demo

Link in TextView Demo

2 thoughts on “TextView with link in ANDROID…….

  1. Pingback: android - Cómo colocar un hipervínculo a una página web en la app de android?

  2. Pingback: android - Come inserire collegamento ipertestuale a un sito web su android?

Leave a Reply

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