Skip to content Skip to sidebar Skip to footer

Android - How To Set The Portion Of The Text Is Clickable

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.

Solution 1:

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

Custom Span Class:

publicclassClickSpanextendsClickableSpan {

    private OnClickListener mListener;

    publicClickSpan(OnClickListener listener) {
        mListener = listener;
    }

    @OverridepublicvoidonClick(View widget) {
       if (mListener != null) mListener.onClick();
    }

    publicinterfaceOnClickListener {
        voidonClick();
    }
}

Helper function:

publicstaticvoidclickify(TextView view, final String clickableText, 
    final ClickSpan.OnClickListener listener) {

    CharSequencetext= view.getText();
    Stringstring= text.toString();
    ClickSpanspan=newClickSpan(listener);

    intstart= string.indexOf(clickableText);
    intend= start + clickableText.length();
    if (start == -1) return;

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

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

Usage:

clickify(textView, clickText,newClickSpan.OnClickListener()
     {
        @OverridepublicvoidonClick() {
            // do something
        }
    });

Solution 2:

try this may it works

SpannableStringspan=newSpannableString(
        "Click here to for gmail page.");


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

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

change start and end position according to your text size

Solution 3:

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..

Solution 4:

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:

publicvoidopenWebView(View v){
    ....
    // Do something
}

Post a Comment for "Android - How To Set The Portion Of The Text Is Clickable"