Solution 1 :
You have declared setWebViewClient twice , ie after load url you again
declared the setWebViewClient with your custom client , correct answer is
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pd.show();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
}
@Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
Solution 2 :
Place a breakpoint in
onPageFinished
and see if the listener actually get called?
Problem :
I’m new to Java and was trying to create a web-view app. The loading widget was created using Progress Dialog but it wouldn’t stop. I don’t know what is wrong. Please help.
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mywebView = (WebView) findViewById(R.id.webview);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setSupportZoom(true);
mywebView.getSettings().setBuiltInZoomControls(true);
mywebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mywebView.loadUrl("https://google.com");
mywebView.setWebViewClient(new WvClient());
}
private class WvClient extends WebViewClient
{
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError er) {
handler.proceed();
}
}
@Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
Comments
Comment posted by Manoj Raj
Thank you for your reply. I tried this but the pd in following code is turning red. I don’t know what that means. @Override public void onPageFinished(WebView view, String url) { if(pd!=null && pd.isShowing()) { pd.dismiss(); } }
Comment posted by Manoj Raj
Now it says that it cannot find the symbol getActivity() in Toast.makeText(getActivity(), “Cannot load page”, Toast.LENGTH_SHORT).show();
Comment posted by Quick learner
remove this line Toast.makeText(getActivity(), “Cannot load page”, Toast.LENGTH_SHORT).show(); , updated my answer
Comment posted by Manoj Raj
I changed it to getApplication() and its working. Hope it doesn’t break anything else ;). Thank you soo much.
Comment posted by Quick learner
i am glad it worked , please upvote and check the tick icon also