9

Android allows content of a URL to be displayed within an application using WebView. However, for some reason it's not working for me. Below is the code that Iam using:

package com.news;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class NewsActivity extends Activity {
    WebView mWebView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new NewsClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.loadUrl("http://www.androidpeople.com");
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();            
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class NewsClient extends WebViewClient {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            System.out.println("URL: " + url);
            view.loadUrl(url);
            return true;
        }
    }
}
Mehul Solanki
  • 1,147
  • 11
  • 15
Abhishek Jain
  • 971
  • 3
  • 13
  • 18

5 Answers5

4

It is obvious!

You are implementing a new WebViewClient in which you are overriding shouldOverrideUrlLoading method. This method is called for each url you are loading. And what are you doing there? You are returning true (which means loading should be overriden) and then beginning to load the same url! Thus, the url loading will never occur.

Just delete that line:

mWebView.setWebViewClient(new NewsClient());
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
Olegas
  • 10,349
  • 8
  • 51
  • 72
0

Add this line: mWebView.setWebViewClient(new NewsClient());

But shouldOverrideUrlLoading should return false.

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
Chella M
  • 392
  • 1
  • 2
  • 15
  • Could you please [edit] in an explanation of why this code answers the question? Code-only answers are [discouraged](http://meta.stackexchange.com/q/148272/274165), because they don't teach the solution. – Nathan Tuggy Sep 30 '15 at 01:54
0

Your code is working in my android device 4.2.2, there is no problem in your implementation, the only thing that you need to do is Override your method like this:

   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
        System.out.println("URL: " + url);
        view.loadUrl(url);
        return true;
    }

This method

0

add this in your android manifest if not added:

<uses-permission android:name="android.permission.INTERNET" />

this line must be inside the <manifest> element of your AndroidManifest.xml file.

mbafford
  • 2,266
  • 1
  • 20
  • 25
Siten
  • 4,515
  • 9
  • 39
  • 64
0

You are implementing a new WebViewClient in which you are overriding shouldOverrideUrlLoading method. If you need more info visit here: https://weblearners.blogspot.com/2021/06/how-to-enable-video-image-and-file-upload-in-android-webview-app.html

  • 2
    shorifa islam, a link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – STA Jun 28 '21 at 05:28