2

I am trying for two days to find a code to work with what I have.. I am following this tutorial: http://techvalleyprojects.blogspot.ro/2011_08_01_archive.html

I have the following problem: The webpage I want to open in my app is not loaded inside my webview, instead it opens in the default browser. How can I modify my code so that all links open in the webview. I need a easy solution because I am very new to Android..

Thank you.

package com.example.name;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class MainActivity extends Activity {
WebView browser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // find the WebView by name in the main.xml of step 2
        browser=(WebView)findViewById(R.id.wvwMain);

        // Enable javascript
        browser.getSettings().setJavaScriptEnabled(true);  

        // Set WebView client
        browser.setWebChromeClient(new WebChromeClient());

        // Load the webpage
        browser.loadUrl("http://news.google.com/");

    }
}
nurealam11
  • 537
  • 4
  • 16
Adrian M.
  • 6,153
  • 15
  • 43
  • 53

2 Answers2

11

You need to implement setWebViewClient(....) like so.

webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
                }
        });

Update:

Make your Activity like so

public class MainActivity extends Activity {
WebView browser;

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // find the WebView by name in the main.xml of step 2
    browser=(WebView)findViewById(R.id.wvwMain);

    // Enable javascript
    browser.getSettings().setJavaScriptEnabled(true);  

    // Set WebView client
    browser.setWebChromeClient(new WebChromeClient());

    browser.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
                }
        });
     // Load the webpage
    browser.loadUrl("http://news.google.com/");
   }
}
M D
  • 46,860
  • 8
  • 87
  • 108
  • Thank you, but where do I put the URL? Do I replace (url) with ("http://news.google.com/") ? – Adrian M. Jun 04 '14 at 10:57
  • @AdrianM. wait i'll up date my answer. – M D Jun 04 '14 at 10:58
  • Thanks, but I am getting 3 errors: -- Description Resource Path Location Type The method shouldOverrideUrlLoading(WebView, String) of type new WebViewClient(){} must override or implement a supertype method MainActivity.java /name/src/com/example/name line 31 Java Problem -- Description Resource Path Location Type webView cannot be resolved MainActivity.java /name/src/com/example/name line 28 Java Problem -- Description Resource Path Location Type WebViewClient cannot be resolved to a type MainActivity.java /name/src/com/example/name line 28 Java Problem – Adrian M. Jun 04 '14 at 11:02
  • @AdrianM. What is that? post it. – M D Jun 04 '14 at 11:03
  • Please see above comment and thank your for your time and suggestions.. – Adrian M. Jun 04 '14 at 11:07
  • @AdrianM. `import android.webkit.WebViewClient;` in your `Activity` and also try this `browser.setWebViewClient(new WebViewClient() {....}` – M D Jun 04 '14 at 11:07
  • 1
    That was the fix. Thank you so so so much :) – Adrian M. Jun 04 '14 at 11:12
  • @AdrianM. yo welcome mate! – M D Jun 04 '14 at 11:13
0

Add this setContentView(browser); instead of setContentView(R.layout.activity_main);, but add it at the end of the method.

Edit:

Also, add this:

   browser.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            }
        });
Tareq
  • 719
  • 1
  • 10
  • 22