0

I'm trying to setup a fragment that mainly displays a progress bar and a webview, once the website loads up completely, the progress bar should disappear. I'm following this code:

https://stackoverflow.com/a/8467430/7803533

The problem is that the OnProgressChanged method is never used:

    public class HUB extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_hub, container, false);

        final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.pBar);

        WebView mWebView = (WebView) rootView.findViewById(R.id.webview);
        mWebView.setWebViewClient(new WebViewClient(){

            public void onProgressChanged(WebView view, int progress){
                if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
                    progressBar.setVisibility(ProgressBar.VISIBLE);
                }

                progressBar.setProgress(progress);
                if(progress == 100){
                    progressBar.setVisibility(ProgressBar.GONE);
                }
            }
        });
        mWebView.clearCache(true);
        mWebView.clearHistory();
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.loadUrl("https:10.10.1.40/index.html");
        return rootView;
    }
}

Layout:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="sykes.moodleapp.HUB">

        <!-- TODO: Update blank fragment layout -->

        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/pBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:padding="2dip"/>

        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webview"
            ></WebView>

    </FrameLayout>
fstennet
  • 1,582
  • 2
  • 12
  • 16

2 Answers2

0

Change your progress bar Visibility to GONE in xml by adding this line:

android:visibility="gone"

Because your code this condition if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE) will only result to true if the progress is less than 100 and visibility is `View.GONE:

OTHERWISE

Just after you have get reference to your Progress bar add a line next to it that the View is GONE This way:

final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.pBar); 
progressBar.setVisibility(View.GONE); //Add this line
Xenolion
  • 12,035
  • 7
  • 33
  • 48
0

For future reference, WebViewClient doesn't have a onProgressChanged() method, WebChromeClient does.

I ended up using them both since I wanted to be able to display SSL errors with onReceivedSslError().

public class HUB extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {


    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_hub, container, false);

    final ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.pBar);

    WebView mWebView = (WebView) rootView.findViewById(R.id.webview);
    mWebView.setWebChromeClient(new WebChromeClient(){
    // SSL error handler
    });

    mWebView.setWebViewClient(new WebViewClient(){

        public void onProgressChanged(WebView view, int progress){
            if(progress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
                progressBar.setVisibility(ProgressBar.VISIBLE);
            }

            progressBar.setProgress(progress);
            if(progress == 100){
                progressBar.setVisibility(ProgressBar.GONE);
            }
        }
    });
    mWebView.clearCache(true);
    mWebView.clearHistory();
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.loadUrl("https:10.10.1.40/index.html");
    return rootView;
}
fstennet
  • 1,582
  • 2
  • 12
  • 16