user1419810 user1419810 - 13 days ago 8
Android Question

Spinner wont dissapear on webview load - android

I'm trying to set a progress spinner (in andorid) to disappear once the webview has loaded but cant get it to work at all, the spinner appears using onPageStarted but doesn't disappear using onPageFinished does anyone know what I'm doing wrong?

public class MainActivity extends AppCompatActivity {

private WebView mWebView;

private ProgressBar spinner;

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

mWebView = (WebView) findViewById(R.id.activity_main_webview);

spinner = (ProgressBar)findViewById(R.id.progressBar1);

//spinner.setVisibility(WebView.VISIBLE);

// Enable Setup Webview
// WebView myWebView = (WebView) findViewById(R.id.myWebView);
mWebView.loadUrl("https://google.com");
// myWebView.setWebViewClient(new MyWebViewClient());

mWebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url) {
spinner.setVisibility(WebView.VISIBLE);
}
public void onPageFinished(WebView view, String url) {
// do your stuff here
spinner.setVisibility(WebView.GONE);
}
});

// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
}

Answer

you are overwriting old webViewClient.

// Remove this because you already set one above which is responsible for showing and hiding progressbar.

mWebView.setWebViewClient(new WebViewClient());