Android ProgessBar while loading WebView

后端 未结 12 2597
闹比i
闹比i 2020-12-08 01:52

In my application, I have a WebView which loads any URL from the internet. Now, sometimes due to slow networks the page takes a long time to load and the user s

12条回答
  •  生来不讨喜
    2020-12-08 02:33

        WebView webView;
    ProgressDialog progressDialog;
    
    TextView text;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
    
    
        String message = getIntent().getStringExtra("key").toString();
        setTitle(message);
        Bundle isNetworkAvailable = getIntent().getExtras();
    
        String value = "file:///android_asset/n/o/i/no_connection.html";
        if (isNetworkAvailable()) {
            value = isNetworkAvailable.getString("keyHTML");
        }
        webView = (WebView) findViewById(R.id.kamal);
        webView.loadUrl(value);
        init();
    }
    
    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
    
    private void init() {
        webView = (WebView) findViewById(R.id.kamal);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.setBackgroundColor(0);
        webView.getSettings().setJavaScriptEnabled(true);
        progressDialog = new ProgressDialog(WebView_Online.this);
        progressDialog.setMessage("Loading Please wait...");
        progressDialog.setCancelable(false);
        progressDialog.show();
        webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                try {
                    progressDialog.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
    
                }
            }
    
        });
    }
    

提交回复
热议问题