Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a link in a webview instead of default browser

I have this piece of code:

    TextView noteView = (TextView) view.findViewById(R.id.content);
    noteView.setMovementMethod(LinkMovementMethod.getInstance());
    noteView.setText(Html.fromHtml(noteView.getText().toString()));

I need to open links in a webview, not in a browser... is this possible?? how can I do??

Thanks in advance..

like image 221
Erenwoid Avatar asked Jul 09 '11 17:07

Erenwoid


2 Answers

additional, you can do this.

    package com.TextHtml;

    import android.app.Activity;  
    import android.content.Context;  
    import android.os.Bundle;  
    import android.text.Html;  
    import android.text.Spannable;  
    import android.text.SpannableStringBuilder;  
    import android.text.method.LinkMovementMethod;  
    import android.text.style.ClickableSpan;  
    import android.text.style.URLSpan;  
    import android.view.View;  
    import android.widget.TextView;  
    import android.widget.Toast;  
    public class TextHtml extends Activity {

        private TextView  tv;  
        static Context ctx=null;  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            ctx=this;  
            tv = (TextView) findViewById(R.id.tv);   
            String htmlLinkText = "<a href="/" mce_href="/""http://www.google.com/"><u>hello google </u></a>";     
            tv.setText(Html.fromHtml(htmlLinkText));  
            tv.setMovementMethod(LinkMovementMethod.getInstance());     
            CharSequence text = tv.getText();     
            if(text instanceof Spannable){     
                int end = text.length();     
                Spannable sp = (Spannable)tv.getText();     
                URLSpan[] urls=sp.getSpans(0, end, URLSpan.class);      
                SpannableStringBuilder style=new SpannableStringBuilder(text);     
                style.clearSpans();//should clear old spans     
                for(URLSpan url : urls){     
                    CustomerTextClick click = new CustomerTextClick(url.getURL());     
                    style.setSpan(click,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);     
                }     
                tv.setText(style);     
            }  
        }  

        private static class CustomerTextClick extends ClickableSpan{     

            private String mUrl;     
            MyURLSpan(String url) {     
                mUrl =url;     
            }     
            @Override  
            public void onClick(View widget) {  
                // TODO Auto-generated method stub  
                Toast.makeText(ctx, "hello google!",Toast.LENGTH_LONG).show();  
            }     
        }  
    } 
like image 95
Chasel Lee Avatar answered Oct 12 '22 07:10

Chasel Lee


Yes, you can do that, it's pretty simple task with WebView, you need to declare a WebViewClient object and override the public boolean shouldOverrideUrlLoading (WebView view, String url) method, there you can filter urls or give some customized functionality.

In your case, to stay on the WebView, you would need to return false on that method.

Check out this tutorial.

Regards

EDITED:

It seems your question is how to handle the click event on the TextView's url. As it's suggested on this question you can filter the ACTION_VIEW intent on your WebView containing Activity. If you need more guidance about intent-filters, check this out.

like image 41
mdelolmo Avatar answered Oct 12 '22 05:10

mdelolmo