Add a Progress Bar in WebView

前端 未结 8 1831
無奈伤痛
無奈伤痛 2020-11-27 15:50

I am trying to add a progress/loading bar to my application that uses WebView. I am confused on how to implement a progress bar that appears every time a link i

8条回答
  •  Happy的楠姐
    2020-11-27 16:35

    I have added few lines in your code and now its working fine with progress bar.

            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.main );
            // Makes Progress bar Visible
            getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
    
            webview = (WebView) findViewById(R.id.webview);
            webview.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress)   
                {
                    //Make the bar disappear after URL is loaded, and changes string to Loading...
                    setTitle("Loading...");
                    setProgress(progress * 100); //Make the bar disappear after URL is loaded
         
                    // Return the app name after finish loading
                    if(progress == 100)
                       setTitle(R.string.app_name);
                    }
                });
            webview.setWebViewClient(new HelloWebViewClient());
            webview.getSettings().setJavaScriptEnabled(true);
            webview.loadUrl("http://www.google.com");
    

提交回复
热议问题