Android WebVIew WebViewClient

14,995

Solution 1

If you never want to open an URL in a browser you have to return false in shouldOverrideUrlLoading

Solution 2

another solution could be :

myWebView.setWebViewClient(new WebViewClient());

by default the default web browser opens and loads the destination URL, to overwrite this behavior, call

webView.setWebViewClient(new WebViewClient());
Share:
14,995

Related videos on Youtube

Droidme
Author by

Droidme

Updated on June 04, 2022

Comments

  • Droidme
    Droidme almost 2 years

    I have a webView in my application. When a person loads a url, the webpage is loaded in the browser and not in my application as the options menu is the default and not what I have assigned. How can I stop this and make it load in my webview and not the browser?

    I tried webViewClient but it doesn't seem to work.

    public class webView extends Activity {
        
        WebView myWebView;
        String url;
          
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
          
            myWebView = (WebView) findViewById(R.id.webview);
            myWebView.getSettings().setJavaScriptEnabled(true);
            url = "http://d.android.com";
            myWebView.setWebViewClient(new WebViewClient()       
            {
                 @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) 
                {
                    
                    //url="http://google.com";
                    //view.loadUrl(url);
                    System.out.println("hello");
                    return true;
                }
            });
            //Toast.makeText(this, "", Toast.LENGTH_SHORT);
            myWebView.loadUrl(url);
        }
    /** Creteing an options menu**/
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            
            MenuInflater inflater = getMenuInflater();
              inflater.inflate(R.menu.menu, menu);
              //return true;
            // TODO Auto-generated method stub
            return super.onCreateOptionsMenu(menu);
        }
    
  • Droidme
    Droidme over 12 years
    Even if i declare a basic webview and load google.com, it loads it using the browser. How can I load it in my webview?
  • Kwenk
    Kwenk over 12 years
    Have you tested it with a real URL without redirection? (I think you added correct permissions to your manifest?)
  • Droidme
    Droidme over 12 years
    its working now. Thank you. Shoul we have our own adress bar for the webview? cause thts how i am getting now.