1

I have one TextView. In this view I want to make it as some portion of text is clickable. if you click on that text then I want to open WebView.

I did the following way:

textView.setText(Html.fromHtml("I have read and agree to the " +
                     "<a href='id.web.freelancer.example.TCActivity://Kode'>TERMS AND CONDITIONS</a>"));
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());

Here if you click on the TERMS AND CONDITIONS then it opens in the browser but I want to open it in the WebView.

Aurora0001
  • 13,139
  • 5
  • 50
  • 53
naresh
  • 10,332
  • 25
  • 81
  • 124

4 Answers4

2

Another way, borrows a bit from Linkify but allows you to customize your handling.

Custom Span Class:

public class ClickSpan extends ClickableSpan {

    private OnClickListener mListener;

    public ClickSpan(OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View widget) {
       if (mListener != null) mListener.onClick();
    }

    public interface OnClickListener {
        void onClick();
    }
}

Helper function:

public static void clickify(TextView view, final String clickableText, 
    final ClickSpan.OnClickListener listener) {

    CharSequence text = view.getText();
    String string = text.toString();
    ClickSpan span = new ClickSpan(listener);

    int start = string.indexOf(clickableText);
    int end = start + clickableText.length();
    if (start == -1) return;

    if (text instanceof Spannable) {
        ((Spannable)text).setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        SpannableString s = SpannableString.valueOf(text);
        s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        view.setText(s);
    }

    MovementMethod m = view.getMovementMethod();
    if ((m == null) || !(m instanceof LinkMovementMethod)) {
        view.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

Usage:

clickify(textView, clickText,new ClickSpan.OnClickListener()
     {
        @Override
        public void onClick() {
            // do something
        }
    });
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
1

try this may it works

   SpannableString span = new SpannableString(
        "Click here to for gmail page.");


    span.setSpan(new URLSpan("http://www.gmail.com"), 6, 10,
               Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView tv = (TextView) findViewById(R.id.text);
    tv.setText(span);
    tv.setMovementMethod(LinkMovementMethod.getInstance());

change start and end position according to your text size

Khan
  • 7,585
  • 3
  • 27
  • 44
0

How do I make links in a TextView clickable?

or u can create a linear layout with horizontal orientation having 2 textviews making second textview clickable..

Community
  • 1
  • 1
sush
  • 476
  • 1
  • 7
  • 20
0

Why don't you make the textView call on onClick method:

<TextView
    ...
    android:onClick"openWebView"
    ...
/>

And then just have a method in your activity called:

public void openWebView (View v) {
    ....
    // Do something
}
ASceresini
  • 419
  • 3
  • 7
  • i want to make the portion of the text is clickable not whole text. for example "Android is a mobile os" is one textview. In this text i want to make "mobile" is clickable rest of the text is non clickable. is it clear? – naresh Apr 27 '12 at 06:50
  • Well you could do as was suggested by Sush below, and make two TextViews, with the second textview only containing the text you want clickable. then the method that it calls can do whatever you like, such as fire an intent to another activity etc – ASceresini Apr 27 '12 at 06:59