2

This similar SO question didn't help.

I have implemented a simple WebView to my MainActivity which loads a particular webpage on app launch. I have a TextView that shows the loading % of current webpage. The code is as below

public class MainActivity extends AppCompatActivity {

WebView webView;
final String homeUrl = "https://example.com";
String lastAccessedUrl = homeUrl;
WebChromeClient webChromeClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    webView = (WebView) findViewById(R.id.browser);
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setBuiltInZoomControls(true);
    settings.setLoadsImagesAutomatically(false);
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    webView.canGoBack();
    webView.canGoForward();
    webView.setWebViewClient(new MyBrowser());
    webView.loadUrl(homeUrl);

    webChromeClient = new WebChromeClient(){
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            TextView textView = (TextView) findViewById(R.id.loadingPercentage);
            textView.setText(""+newProgress);
        }
    };
    webView.setWebChromeClient(webChromeClient);
}

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        lastAccessedUrl = url;
        view.loadUrl(url);
        return true;
    }
}
}

The problem I am facing is, the TextView shows loading % only for the first time the webpage loads. Then it is set to 100 permanently. Then if I click on any link on the first page, the TextView does not start from zero.

Do justify in comments if you vote down. Any help would be appreciated.

Community
  • 1
  • 1
  • 1
    I don't think this is what is causing your problem, but you should put `TextView textView = (TextView) findViewById(R.id.loadingPercentage);` in `onCreate()` outside of the progress change callback. If you find the view once for the activity you can continue to change the text in it without having to find it again every time. – FoamyGuy Nov 24 '15 at 16:23
  • @FoamyGuy That's obvious and silly mistake. Thank you for pointing it out. :). –  Nov 24 '15 at 16:26

1 Answers1

0

There is no such problem with your code. Make sure you are clicking on a url. If you click on an expand view, it will just expand to show you certain data. It wont load as a new url. Click on say, www.stackoverflow.com and check if it's showing you loading percentage.

Srujan Barai
  • 2,295
  • 4
  • 29
  • 52