-1

progress bar will not stop and will load indefinitely.

Referred Page: progress bar in android webview

fragment code

public class testFragment extends Fragment {

    private WebView mWebView;
    private WebSettings mWebSettings;
    private ProgressBar progressBar;

    public testFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.testfragment, container, false);

        mWebView = (WebView) v.findViewById(R.id.webView);
        progressBar = (ProgressBar) v.findViewById(R.id.pro);
//progressBar.setVisibility(View.GONE);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
            }

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

        mWebView.setWebViewClient(new WebViewClient());
        mWebSettings = mWebView.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setSupportMultipleWindows(false);
        mWebSettings.setJavaScriptCanOpenWindowsAutomatically(false);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setSupportZoom(false);
        mWebSettings.setBuiltInZoomControls(true);
        mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebSettings.setDomStorageEnabled(true);
        mWebView.loadUrl("https://stackoverflow.com/");

        return v;
    }
}

Removing the annotation from the annotation will completely disappear the progress bar.

I also wanted to use @Override public void onProgressChanged (WebView view, int newProgress) on the page referenced, but it did not appear in the available methods.

appdev
  • 15
  • 4
  • You are setting 2 web clients. can you try by removing mWebView.setWebViewClient(new WebViewClient()); – Kishan Maurya Mar 25 '21 at 09:13
  • So the issue is, you have set 2 web clients. and the last statement was overriding first one. So visible & hide was never executed. since in UI, you have set visibility for progress bar as visible .so it is always visible. and code is never reaching in gone one. – Kishan Maurya Mar 25 '21 at 09:18

1 Answers1

1

The issue is, you have set 2 web clients and the last statement was overriding the first one. So visible & hide was never executed.

Since in UI, you have set visibility for progress bar as visible .So it is always visible.

Remove mWebView.setWebViewClient(new WebViewClient()); line.

For onProgressChanged()

You have to use WebChromeClient. Since is available in Chrome Client, not on WebViewClient

mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });
Kishan Maurya
  • 3,356
  • 8
  • 21