简体   繁体   中英

How to open any web link(without any URL scheme) in app's webview activity in-spite of default browser

I have seen some good answers regarding to my question but none of the solutions is 100% correct. most of the solution says setting hyperlink with custom scheme and setting the same scheme in IntentFilter.

But I am developing a message app, User may send any random text like google.com, yahoo.com, etc. Now I set the property autoLink=true in xml so all google.com's becoming hyperlink and when user clicks on it is opening in web-browser. I want this behavior to be changed, so that when user clicks on any link it should open new webview which in an activity belongs same app.

solution1

solution2

Use the following code

public class MainActivity extends Activity {
    private ProgressBar progressBar;
    private WebView webView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setMax(100);

        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        WebSettings settings = webView.getSettings();
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        settings.setDisplayZoomControls(false);

        webView.setWebViewClient(new WebViewClientDemo());
        webView.setWebChromeClient(new WebChromeClientDemo());
        webView.loadUrl("http://business-new.zakoopi.com/#/login/signin");

    }

    private class WebViewClientDemo extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
            progressBar.setProgress(100);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(0);
        }
    }

    private class WebChromeClientDemo extends WebChromeClient {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
        }


        @Override
        public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
            //Required functionality here
            return super.onJsAlert(view, url, message, result);
        }

    }


}

You may need to use Linkify here.

String text = "These are some sample links:some random links";
Spannable spannable = new SpannableString( Html.fromHtml(text) );
Linkify.addLinks(spannable, Linkify.WEB_URLS);

Now you have to listen to the clicks. You can do that like this.

URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
for (URLSpan urlSpan : spans) {
    LinkSpan linkSpan = new LinkSpan(urlSpan.getURL());
    int spanStart = spannable.getSpanStart(urlSpan);
    int spanEnd = spannable.getSpanEnd(urlSpan);
    spannable.setSpan(linkSpan, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.removeSpan(urlSpan);
}

Also Create the LinkSpan class

 private class LinkSpan extends URLSpan {
        private LinkSpan(String url) {
            super(url);
        }

        @Override
        public void onClick(View view) {
            String url = getURL();
            if (url != null) {

            startActivity(new Intent(LinkTestActivity.this,YourWebactivity.class).putExtra("url",url));

        }
    }
}

This solution actually worked for me. But Onclick event is triggering twice, But I am handling it.

private boolean isClickingLink = false;

TextView textView = (TextView) findViewById(R.id.main_text); textView.setMovementMethod(LinkMovementMethod.getInstance());

CharSequence charSequence = textView.getText();
SpannableStringBuilder sp = new SpannableStringBuilder(charSequence);

URLSpan[] spans = sp.getSpans(0, charSequence.length(), URLSpan.class);

for (URLSpan urlSpan : spans) {
    CustomSpan customSpan= new CustomSpan (urlSpan.getURL());
    sp.setSpan(customSpan, sp.getSpanStart(urlSpan),
            sp.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

textView.setText(sp);

textView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // 2.if clicking a link
        if (!isClickingLink) {
            Log.w("log", "not clicking link");
        }
        isClickingLink = false;
    }
});

private class CustomSpan extends ClickableSpan {

private String url;

public CustomSpan (String url) {

    super();
    thid.url= url;
}

@Override
public void onClick(View widget) {

    isClickingLink = true;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM