make TextView Fake Link using SpannableString - Android User Interface

Android examples for User Interface:TextView

Description

make TextView Fake Link using SpannableString

Demo Code


//package com.java2s;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

public class Main {
    public static void makeTextViewFakeLink(TextView tv,
            final View.OnClickListener onClickListener) {
        makeTextViewFakeLink(tv, onClickListener,
                android.R.drawable.list_selector_background);
    }/*from  www  .  j a  v  a2 s. c o m*/

    public static void makeTextViewFakeLink(TextView tv,
            final View.OnClickListener onClickListener, int selectorBgResId) {
        String text = tv.getText().toString();
        SpannableString spanString = new SpannableString(text);
        ClickableSpan clickSpan = new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                //            onClickListener.onClick(widget);
            }
        };
        spanString.setSpan(clickSpan, 0, text.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv.setLinksClickable(false);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setBackgroundResource(selectorBgResId);
        tv.setText(spanString);
        tv.setOnClickListener(onClickListener);
    }
}

Related Tutorials