简体   繁体   中英

How to add progress bar line in webview

I am trying to add a progress bar or loading bar to my application which uses webview. I have to application set up to run all the links within the app but i am confused on how to implement a progress bar for whenever a link is clicked on

this is my code public class MainActivity extends ActionBarActivity {

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://caknowledge.in");
    mWebView.setWebViewClient(new MyAppWebViewClient());
}

@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private class MyAppWebViewClient extends WebViewClient {
}

And XML.JAVA Code is as below

<WebView android:id="@+id/activity_main_webview" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

只需使用webview上方的progressBar,然后在web view开始加载页面时使其可见,并在webview页面加载时隐藏progressbar

you can add below code on your webviewclient to show progress dialog

private class MyAppWebViewClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView p_view, String p_url, Bitmap p_favIcon)
    {
        try {
            if (progressDialog != null && !isFinishing())
                progressDialog.show();
        } catch (Throwable ex) {
            //ignore
        }
        super.onPageStarted(p_view, p_url, p_favIcon);
    }
    @Override
    public void onPageFinished(WebView p_view, String p_url)
    {
        try {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        } catch (Throwable ex) {
            //ignore
        }
        super.onPageFinished(p_view, p_url);
    }
}

Progress bar declaration in activity

progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading..");

Hope it works for you !!

layout code

  <ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/progress_bar"
     style="?android:attr/progressBarStyleHorizontal"/>

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

java code

webView.setWebViewClient(new HelloWebViewClient()); 
progressBar.setVisibility(View.INVISIBLE);


private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon)
    {
        // TODO show you progress image

        progressBar.setVisibility(View.VISIBLE);

        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url)
    {    progressBar.setVisibility(View.INVISIBLE);
        // TODO hide your progress image
        super.onPageFinished(view, url);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM