user1767260
user1767260

Reputation: 1561

Enabling click action for links in Android Textview

I need to enable click action for links in Textview and open the webpage in app window. Linkify will open in browser.But I want to open in a webview in my app. Please help me.

Upvotes: 0

Views: 220

Answers (2)

Steve Bergamini
Steve Bergamini

Reputation: 14600

There's a blog post that talks about extending ClickableSpan.

First extending the ClickableSpan class:

static class InternalURLSpan extends ClickableSpan {
    OnClickListener mListener;

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

    @Override
    public void onClick(View widget) {
        mListener.onClick(widget);
    }
}

The blog talks about the clickable link doing something in your activity. You can easily adapt this approach to open the URL in a WebView.

It would essentially look something like this:

SpannableString ss = new SpannableString("....")
ss.setSpan(new InternalURLSpan(new OnClickListener() {
        public void onClick(View v) {
            // Your code to open the link in a WebView here.
        }
    }), x, y, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

You would just set the SpannableString object as the text of your TextView:

textView.setText(ss);

Upvotes: 1

Vaibhav Agarwal
Vaibhav Agarwal

Reputation: 4499

You can underline textview through string.xml file

<string name="hello_world"><u>Hello world!<u></string>

Now you can set onClickListener on your textView and can redirect user to your webView Activity instead of browser app.

Upvotes: 0

Related Questions