SoFunction
Updated on 2025-03-11

An example of Android implementing a WebView with progress bar

An example of Android implementing a WebView with progress bar

1. WebView loading method

//Load local resourcesloadUrl("file:///android_asset/");
//Load network resourcesloadUrl("");

2. Drawable file view_progress_webview with progress

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:andro>

  <item android:>
    <clip>
      <shape>
        <solid android:color="#31CE15"/>
        <corners android:radius="2dp"/>
      </shape>
    </clip>
  </item>

</layer-list>

The color value is progress color, change as needed

3. ProgressWebView class

/**
  * WebView with progress bar
  * @Author GQ
  */
public class ProgressWebView extends WebView {

  private ProgressBar progressbar;
  private Context mContext;

  public ProgressWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
     = context;
    progressbar = new ProgressBar(context, null, );
    (new LayoutParams(LayoutParams.FILL_PARENT, 5, 0, 0));

    Drawable drawable = ().getDrawable(.view_progress_webview);
    (drawable);
    addView(progressbar);

    //Mainly deal with the things done by browsers such as parsing and rendering web pages    setWebViewClient(new WebViewClient() {
      @Override
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        (view, errorCode, description, failingUrl);
        // When loading fails, display the customized page        if (errorListener != null) {
          ();
        }
      }
    });
    //Assisting WebView to handle Javascript dialog boxes, website icons, website titles, loading progress, etc.    setWebChromeClient(new WebChromeClient());

    getSettings().setSupportZoom(true);//Is it possible to zoom    getSettings().setBuiltInZoomControls(true);
    getSettings().setJavaScriptEnabled(true);//Support JS    getSettings().setLayoutAlgorithm(.NARROW_COLUMNS);
    getSettings().setUseWideViewPort(true);
    getSettings().setLoadWithOverviewMode(true);
    getSettings().setSaveFormData(true);
    getSettings().setDomStorageEnabled(true);

    //Preferring to cache    getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

    //Disable long press    setOnLongClickListener(new OnLongClickListener() {
      @Override
      public boolean onLongClick(View view) {
        return true;
      }
    });

    //If downloaded in the browser, call the browser default download + notification bar    setDownloadListener(new DownloadListener() {
      @Override
      public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
        Uri uri = (url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        (intent);
      }
    });
  }


  public class WebChromeClient extends  {
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
      if (newProgress == 100) {
        (GONE);
      } else {
        if (() == GONE)
          (VISIBLE);
        (newProgress);
      }
      (view, newProgress);
    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
      (view, title);
      if (titleListener != null)
        (title);
    }

  }


  @Override
  protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    LayoutParams lp = (LayoutParams) ();
     = l;
     = t;
    (lp);
    (l, t, oldl, oldt);
  }

  private TitleListener titleListener;

  public interface TitleListener {
    void getTitle(String title);
  }

  public void setOnTitleListener(TitleListener titleListener) {
     = titleListener;
  }

  private ErrorListener errorListener;

  public interface ErrorListener {
    void onError();
  }

  public void setOnErrorListener(ErrorListener errorListener) {
     = errorListener;
  }

}

4. Use

/**
 * Public WebView
 */
public class BasicWebActivity extends Activity {

  protected ProgressWebView progressWebView;
  private TextView title;//Title bar  private TextView tv_none;//Loading failed to display text
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.common_webview);

    title = (TextView) findViewById();
    tv_none = (TextView) findViewById(.tv_none);
    progressWebView = (ProgressWebView) findViewById();

    String url = getIntent().getStringExtra("url");
    (new () {
      @Override
      public void getTitle(String title) {
        (title);
      }
    });
    (new () {
      @Override
      public void onError() {
        tv_none.setText("url resource invalid");
      }
    });
    //Load the web page    (url);
  }

 //Rewrite the return key  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      if (()) {
        ();//Return to the previous page        return true;
      } else {
        finish();//Close the page      }
    }
    return (keyCode, event);
  }
}

The common_webview contains a title and a progressWebView and will not paste the code.

If you have any questions, please leave a message or go to the community of this website to communicate and discuss. There are many articles on Android development on this website. I hope you can search and refer to it. Thank you for your reading. I hope it can help you. Thank you for your support for this website!