Turns a textview into a clickable hyperlink with the provided text and attaches an OnClickListener. - Android User Interface

Android examples for User Interface:TextView

Description

Turns a textview into a clickable hyperlink with the provided text and attaches an OnClickListener.

Demo Code


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import com.android.smap.R;

public class Main{
    /**//from  w  ww .  j  a  v  a 2  s . co  m
     * Turns a textview into a clickable hyperlink with the provided text and attaches an
     * OnClickListener.
     * 
     * @param text
     * @param view
     * @param lis
     */
    public static void makeHyperlink(String text, TextView view,
            OnClickListener lis) {
        SpannableString ss = new SpannableString(text);

        ss.setSpan(new UnderlineSpan(), 0, ss.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        view.setText(ss);
        view.setMovementMethod(LinkMovementMethod.getInstance());
        view.setOnClickListener(lis);
    }
}

Related Tutorials