1

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

Community
  • 1
  • 1
Pruthviraj
  • 560
  • 6
  • 23

3 Answers3

0

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);
        }

    }


}
mdDroid
  • 3,135
  • 2
  • 22
  • 34
  • Hi, I know how to create web-view in android and loading of any URL in it. What I want to know is, I have recyclreview and all the rows of it gets created dynamically as user sends messages. User might send any message that might contain any url type text like googl.com, yahoo.com, etc Right now all these texts are appearing in different color and taking click, When user clicks on it, it is opening in default browser. I want to override this normal behaviour of opening in browser. – Pruthviraj May 26 '16 at 09:56
0

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));

        }
    }
}
Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53
  • Hi I have tried the above solution but not working for me. Following is my code Spannable spannable = new SpannableString("Hi dude google.com"); Linkify.addLinks(spannable, Linkify.WEB_URLS); dude to space constraint I put only imp lines – Pruthviraj May 26 '16 at 10:51
  • What was the issue ? Links not detected ? "Html.fromHtml(text)" this is also imp.. Can you try this after adding that also – Sunil Sunny May 26 '16 at 10:52
  • Link is detected. On clicking link is opening in default browser. I am not getting click event callback in LinkSpan's onClick method. Yes tried Html.fromHtml(text). – Pruthviraj May 26 '16 at 10:54
  • ok can you add http:// to your link .I mean not just google.com the whole address. I think Linkify works only when http:// is in your url .. – Sunil Sunny May 26 '16 at 11:00
  • Android is able to differentiate it even without and http:// and opening the link in default browser. I want same to be happen using my webview. It is very unlikely that a user types http://google.com, – Pruthviraj May 26 '16 at 11:03
0

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;
}

}

Pruthviraj
  • 560
  • 6
  • 23