开发者

show progress dialog on webview

In my android application i am opening a webview.I want to hide url that is getting loaded,so the default window progress bar doesnot work for me.

Is there any way than that i can add progress dialogue on webview.

I am using the below code.

 mWebView = (WebView) findViewById(R.id.webview);
     mWebView.getSettings().setJavaScriptEnabled(true); 

     final Activity activity = this; 

     mWebView.setWebChromeClient(new WebChromeClient(){ 

     public void onProgressChanged(WebView view, int progress) { 
  开发者_如何学运维   activity.setTitle("Loading..."); 
     activity.setProgress(progress * 100); 
     if(progress == 100) 
     activity.setTitle("My title"); 
     } 
     }); 

     mWebView.loadUrl(Url);
     mWebView.setWebViewClient(new HelloWebViewClient());
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

private class HelloWebViewClient extends WebViewClient {   
    ProgressDialog MyDialog=new ProgressDialog(context);

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

Please share your valuable suggestions.

Thanks in advance :)


if you hide application title by "android:theme="@android:style/Theme.NoTitleBar"" from manifest then you won't see progress bar which is being displayed in title by this code:

    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);               
    setContentView(R.layout.main);

// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

So you can show progress bar with a Dialog box like this inside the onCreate():

final Activity activity = this;

final ProgressDialog progressDialog = new ProgressDialog(activity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);

browser.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        progressDialog.show();
        progressDialog.setProgress(0);
        activity.setProgress(progress * 1000);

        progressDialog.incrementProgressBy(progress);

        if(progress == 100 && progressDialog.isShowing())
            progressDialog.dismiss();
    }
});


Since ProgressDialog is deprecated in API level 26 (Oreo), I will show how to use ProgressBar with WebView :

Step 1) Create a FrameLayout with WebView and ProgressBar like this:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fl"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />
    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />
</FrameLayout>

Step 2) Add the following code inside onCreate() like this:

    progressBar = findViewById(R.id.progress);
    webView = findViewById(R.id.web_view);

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int newProgress) {

            // Keep WebView hidden when the url is loading
            webView.setVisibility(View.INVISIBLE);

            // Make ProgressBar visible
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(0);
            progressBar.incrementProgressBy(newProgress);

            if(newProgress == 100 && progressBar.getVisibility() == View.VISIBLE) {
               // Make ProgressBar invisible and WebView visible
                progressBar.setVisibility(View.INVISIBLE);
                    webView.setVisibility(View.VISIBLE);
                }
    }});

    webView.loadUrl(URL);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜