13

I'm already active JavaScript for a given WebView, and opens new link inside the WebView, not in the Browser. This Is Main Activity

    package com.Afrogfx.pronouns;

    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.view.Menu;
    import android.webkit.WebView;

    @SuppressLint("SetJavaScriptEnabled")
    public class MainActivityPronouns extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity_pronouns);

    WebView wvHtml = (WebView) findViewById(R.id.webview);
    wvHtml.getSettings().setBuiltInZoomControls(true);
    wvHtml.getSettings().setJavaScriptEnabled(true);
    wvHtml.loadUrl("http://afrogfx.com/appcatcategories.php?catid=13&parentid=11");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_activity_pronouns, menu);
    return true;
    }
    }

how can i handel my code to open all link in site inside the WebView (App) , not in the Browser & ( don't show to user Open in browser).

Afro Gfx
  • 195
  • 1
  • 1
  • 7
  • 1
    possible duplicate of [Clicking URLs opens default browser](http://stackoverflow.com/questions/2378800/clicking-urls-opens-default-browser) – Confuse Oct 03 '14 at 09:36

3 Answers3

29

For that just create the subclass that is extending webclient and use the method of that class onPageFinished(WebView c,String url) and
public boolean shouldOverrideUrlLoading(final WebView view, final String url)

here is the code-

 myWebView.setWebViewClient(new WebViewClient()       
        {
             @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) 
            {
                //view.loadUrl(url);
                System.out.println("hello");
                return false;
            }
        });
        myWebView.loadUrl(url);
hugo der hungrige
  • 12,382
  • 9
  • 57
  • 84
Ravi
  • 2,277
  • 3
  • 22
  • 37
10

In case you use Kotlin:

webView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    return false
                }
            }
Dievskiy
  • 199
  • 3
  • 4
-2

so you have to use WebviewClicent .. here is the class

private class HelloWebViewClient extends WebViewClient 
{
    @Override
    public boolean shouldOverrideUrlLoading(final WebView view, final String url) 
    {
        Utils.showActivityViewer(WebsiteActivity.this);
        new Thread(new Runnable() 
        {

            public void run()
            {
                view.loadUrl(url);
            }



            }).start();  



        return true;
    }
}

and bind in a webview as

webview.setWebViewClient(new HelloWebViewClient());

and refer http://developer.android.com/reference/android/webkit/WebViewClient.html

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
  • 3
    Welcome to "Only the original thread that created a view hierarchy can touch its views.” exception ) – Stan Dec 02 '15 at 16:49