Example usage for android.webkit WebChromeClient WebChromeClient

List of usage examples for android.webkit WebChromeClient WebChromeClient

Introduction

In this page you can find the example usage for android.webkit WebChromeClient WebChromeClient.

Prototype

WebChromeClient

Source Link

Usage

From source file:in.ac.dtu.subtlenews.InstapaperViewer.java

@SuppressLint("NewApi")
@Override// w w  w . j a  v  a2  s. c o  m
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        setImmersive(true);
    }
    setTheme(R.style.AppTheme_NoActionBar);

    setContentView(R.layout.activity_instapaper_viewer);
    //setupActionBar();

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        urlId = extras.getInt("URLID");
        urlList = extras.getStringArrayList("URLList");

    }
    final WebView instapaperView = (WebView) findViewById(R.id.instapaper_viewer);
    final Activity activity = this;
    final ProgressBar progressBar;
    progressBar = (ProgressBar) findViewById(R.id.progressBar);

    instapaperView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            if (progress < 100 && progressBar.getVisibility() == ProgressBar.GONE) {
                progressBar.setVisibility(ProgressBar.VISIBLE);
                //txtview.setVisibility(View.VISIBLE);
            }
            progressBar.setProgress(progress);
            if (progress == 100) {
                progressBar.setVisibility(ProgressBar.GONE);
                //txtview.setVisibility(View.GONE);
            }
        }
    });

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.instapaper_viewer);

    final Button prevButton = (Button) findViewById(R.id.summary_prev_button);
    final Button nextButton = (Button) findViewById(R.id.summary_next_button);

    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
        // Cached values.
        int mControlsHeight;
        int mShortAnimTime;

        @Override
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
        public void onVisibilityChange(boolean visible) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                // If the ViewPropertyAnimator API is available
                // (Honeycomb MR2 and later), use it to animate the
                // in-layout UI controls at the bottom of the
                // screen.
                if (mControlsHeight == 0) {
                    mControlsHeight = controlsView.getHeight();
                }
                if (mShortAnimTime == 0) {
                    mShortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
                }
                controlsView.animate().translationY(visible ? 0 : mControlsHeight).setDuration(mShortAnimTime);
            } else {
                // If the ViewPropertyAnimator APIs aren't
                // available, simply show or hide the in-layout UI
                // controls.
                controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
            }

            if (visible && AUTO_HIDE) {
                // Schedule a hide().
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            }

        }
    });

    // Set up the user interaction to manually show or hide the system UI.
    contentView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TOGGLE_ON_CLICK) {
                mSystemUiHider.toggle();
            } else {
                mSystemUiHider.show();
            }
        }
    });

    // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
    prevButton.setOnTouchListener(mDelayHideTouchListener);
    nextButton.setOnTouchListener(mDelayHideTouchListener);

    try {
        summaryUrl = "http://www.instapaper.com/text?u=" + urlList.get(urlId);
    } catch (Exception e) {
        e.printStackTrace();
    }

    instapaperView.loadUrl(summaryUrl);
    prevButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                summaryUrl = "http://www.instapaper.com/text?u=" + urlList.get(--urlId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            instapaperView.loadUrl(summaryUrl);
        }
    });
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                summaryUrl = "http://www.instapaper.com/text?u=" + urlList.get(++urlId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            instapaperView.loadUrl(summaryUrl);
        }
    });

}

From source file:de.zell.android.util.fragments.WebviewFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_webview, container, false);
    if (url != null && !url.isEmpty()) {
        content = ((WebView) rootView.findViewById(R.id.webview));
        //WebViewClient is enabled in order to force all links to load within the webview and also
        // in order to enable JavaScript
        content.setWebViewClient(new HelloWebViewClient());
        WebSettings webSettings = content.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);

        final ProgressBar progressBar = ((MainNavigationActivity) getActivity()).getProgressBar();
        content.setWebChromeClient(new WebChromeClient() {
            @Override/*from   w w w . j a  v  a2  s  .c o  m*/
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
                if (newProgress == 100)
                    progressBar.setVisibility(View.GONE);
            }
        });

        progressBar.setIndeterminate(true);
        progressBar.setVisibility(View.VISIBLE);
        content.loadUrl(url);
    }

    getActivity().setTitle(title);
    return rootView;
}

From source file:net.olejon.mdapp.MedicationNlhFragment.java

@SuppressLint("SetJavaScriptEnabled")
@Override/* w  w  w  .j a va  2 s  .c o m*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_medication_nlh, container, false);

    // Activity
    final Activity activity = getActivity();

    // Context
    final Context context = activity.getApplicationContext();

    // Tools
    final MyTools mTools = new MyTools(context);

    // Arguments
    Bundle bundle = getArguments();

    final String pageUri = bundle.getString("uri");

    // Progress bar
    final ProgressBar progressBar = (ProgressBar) activity
            .findViewById(R.id.medication_toolbar_progressbar_horizontal);

    // Toolbar
    final LinearLayout toolbarSearchLayout = (LinearLayout) activity
            .findViewById(R.id.medication_toolbar_search_layout);
    final EditText toolbarSearchEditText = (EditText) activity.findViewById(R.id.medication_toolbar_search);

    // Web view
    WEBVIEW = (WebView) viewGroup.findViewById(R.id.medication_nlh_content);

    WebSettings webSettings = WEBVIEW.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setAppCachePath(context.getCacheDir().getAbsolutePath());
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

    WEBVIEW.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!mTools.isDeviceConnected()) {
                mTools.showToast(getString(R.string.device_not_connected), 0);
                return true;
            } else if (url.matches(".*/[^#]+#[^/]+$")) {
                WEBVIEW.loadUrl(url.replaceAll("#[^/]+$", ""));
                return true;
            } else if (url.matches("^https?://.*?\\.pdf$")) {
                mTools.downloadFile(view.getTitle(), url);
                return true;
            }

            return false;
        }
    });

    WEBVIEW.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressBar.setVisibility(View.INVISIBLE);
            } else {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setProgress(newProgress);

                toolbarSearchLayout.setVisibility(View.GONE);
                toolbarSearchEditText.setText("");
            }
        }
    });

    if (savedInstanceState == null) {
        WEBVIEW.loadUrl(pageUri);
    } else {
        WEBVIEW.restoreState(savedInstanceState);
    }

    return viewGroup;
}

From source file:io.github.hidroh.materialistic.WebFragment.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    if (mIsHackerNewsUrl) {
        return createLocalView(container, savedInstanceState);
    }/*from   w w w  .  j av a 2  s.c  om*/

    final View view = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_web, container, false);
    mScrollView = (NestedScrollView) view.findViewById(R.id.nested_scroll_view);
    final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress);
    mWebView = (WebView) view.findViewById(R.id.web_view);
    mWebView.setBackgroundColor(Color.TRANSPARENT);
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(newProgress);
            if (newProgress == 100) {
                progressBar.setVisibility(View.GONE);
                mWebView.setBackgroundColor(Color.WHITE);
                mWebView.setVisibility(mExternalRequired ? View.GONE : View.VISIBLE);
            }
        }
    });
    mWebView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
                long contentLength) {
            if (getActivity() == null) {
                return;
            }
            final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            if (intent.resolveActivity(getActivity().getPackageManager()) == null) {
                return;
            }
            mExternalRequired = true;
            mWebView.setVisibility(View.GONE);
            view.findViewById(R.id.empty).setVisibility(View.VISIBLE);
            view.findViewById(R.id.download_button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(intent);
                }
            });
        }
    });
    mWebView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                    return true;
                }
            }
            return false;
        }
    });
    setWebViewSettings(mWebView.getSettings());
    return view;
}

From source file:com.wpi.assistments.HomeTabbedActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_webview);

    final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...", true);

    isTeacher = false;/*from   w  w w  .  j  av a  2s . c  om*/
    toggle = true;
    logoutFlag = false;

    homeWebView = (WebView) findViewById(R.id.homeWebView);
    homeWebView.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url) {
            Log.i("test", view.getUrl());
            if (view.getUrl().equals("https://www.assistments.org/account/login") && !logoutFlag) {

                Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                intent.putExtra("showPopup", "true");
                startActivity(intent);
                finish();
            } else if (view.getUrl().equals("https://www.assistments.org/teacher")) {
                isTeacher = true;
            } else if (view.getUrl().equals("https://www.assistments.org/account/login") && logoutFlag) {
                Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(intent);
            }

            if (!homeWebView.canGoBack()) {
                btnBack.setEnabled(false);
            } else {
                btnBack.setEnabled(true);
            }

            if (!homeWebView.canGoForward()) {
                btnForward.setEnabled(false);
            } else {
                btnForward.setEnabled(true);
            }

            if (pd.isShowing() && pd != null) {
                pd.dismiss();
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            pd.show();
            view.loadUrl(url);
            return true;
        }
    });
    homeWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            new AlertDialog.Builder(webviewContext).setTitle("ASSISTments").setMessage(message)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            logoutFlag = true;
                            result.confirm();
                        }
                    }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            result.cancel();
                        }
                    }).create().show();

            return true;
        }
    });

    if (savedInstanceState != null) {
        homeWebView.restoreState(savedInstanceState);
    } else {
        /** Scaling, replaced by overview mode and wide view port
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        if (size.x <= 780) {
           int scaleRate = (int) ((size.x) / 7.8);
           Log.i("test", Integer.toString(size.x));
           homeWebView.setInitialScale(scaleRate);
        }
        */

        Intent intent = getIntent();

        if (intent.getStringExtra("username").equals("") && intent.getStringExtra("password").equals("")) {
            homeWebView.loadUrl("https://www.assistments.org/signup");

        } else {
            String username = intent.getStringExtra("username");
            String password = intent.getStringExtra("password");

            String postData = "login=" + username + "&password=" + password + "&commit=Log in";
            homeWebView.postUrl("https://www.assistments.org/account/login",
                    EncodingUtils.getBytes(postData, "BASE64"));
        }

    }

    WebSettings settings = homeWebView.getSettings();
    settings.setSaveFormData(false);
    settings.setSavePassword(false);
    settings.setJavaScriptEnabled(true);
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setSupportMultipleWindows(true);
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setAppCacheEnabled(false);
    settings.setDomStorageEnabled(true);
    settings.setDatabaseEnabled(true);
    settings.setUseWideViewPort(false);
    settings.setLoadWithOverviewMode(true);
    homeWebView.clearCache(true);
    homeWebView.setPadding(0, 0, 0, 0);
    homeWebView.setInitialScale(getScale());

    btnBack = (Button) findViewById(R.id.btnBack);
    btnBack.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            homeWebView.goBack();
        }
    });

    btnRefresh = (Button) findViewById(R.id.btnRefresh);
    btnRefresh.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //homeWebView.zoomOut();
            homeWebView.scrollTo(1000, 0);
            Log.i("test", "111");
            //homeWebView.reload();
        }
    });

    btnForward = (Button) findViewById(R.id.btnForward);
    btnForward.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            homeWebView.goForward();
        }
    });

    btnOffline = (Button) findViewById(R.id.btnOffline);
    btnOffline.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (toggle) {
                homeWebView.loadUrl(
                        "https://www.assistments.org/assistments/student/index.html#offlineUserAssignmentList/");
                btnOffline.setText("Home");
                toggle = false;
            } else {
                if (isTeacher) {
                    homeWebView.loadUrl("https://www.assistments.org/teacher");
                } else {
                    homeWebView.loadUrl("https://www.assistments.org/tutor");
                }
                btnOffline.setText("Offline");
                toggle = true;
            }

        }
    });
}

From source file:com.cjones.taskforcemamba.activity.WebActivity.java

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

    mTitle = mDrawerTitle = getTitle();//w  w w . j  a  v a  2s  . c  o m

    //WebView
    Intent launchingIntent = getIntent();
    //        String content = launchingIntent.getData().toString();
    String home = "http://www.tfmamba.com";
    //        Log.i(TAG, "Content = " + content + " . Home = " + home);
    //        if(content == home){
    //            content = content + "&styleid=2";
    //            Log.i(TAG, "IF Statement - Content = " + content + " . Home = " + home);
    //        } else {
    //            Log.i(TAG, "Else Statement - Content = " + content + " . Home = " + home);
    //        }
    //        Log.i(TAG, "conte = " + content);
    myWebView = (WebView) findViewById(R.id.webview);
    //            if(content == null){
    myWebView.loadUrl(home);
    //        }else {
    //                myWebView.loadUrl(content);
    //            }
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    final ProgressBar Pbar;
    Pbar = (ProgressBar) findViewById(R.id.pB1);

    myWebView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            if (progress < 100 && Pbar.getVisibility() == ProgressBar.GONE) {
                Pbar.setVisibility(ProgressBar.VISIBLE);
            }
            Pbar.setProgress(progress);
            if (progress == 100) {
                Pbar.setVisibility(ProgressBar.GONE);
            }
        }
    });
    if (savedInstanceState != null) {
        myWebView.restoreState(savedInstanceState);
    } else {
        //            if(content == null && !content.isEmpty()){
        //            myWebView.loadUrl("http://www.tfmamba.com");
    }
}

From source file:io.github.hidroh.materialistic.SubmitActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppUtils.setStatusBarColor(getWindow(), ContextCompat.getColor(this, R.color.blackT12));
    setContentView(R.layout.activity_submit);
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    //noinspection ConstantConditions
    getSupportActionBar().setDisplayOptions(
            ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_HOME_AS_UP);
    mTitleLayout = (TextInputLayout) findViewById(R.id.textinput_title);
    mContentLayout = (TextInputLayout) findViewById(R.id.textinput_content);
    mTitleEditText = (TextView) findViewById(R.id.edittext_title);
    mContentEditText = (TextView) findViewById(R.id.edittext_content);
    String text, subject;// w  ww. ja v a2 s  .c  o m
    if (savedInstanceState == null) {
        subject = getIntent().getStringExtra(Intent.EXTRA_SUBJECT);
        text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
    } else {
        subject = savedInstanceState.getString(STATE_SUBJECT);
        text = savedInstanceState.getString(STATE_TEXT);
    }
    mTitleEditText.setText(subject);
    mContentEditText.setText(text);
    if (TextUtils.isEmpty(subject)) {
        if (isUrl(text)) {
            WebView webView = new WebView(this);
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onReceivedTitle(WebView view, String title) {
                    if (mTitleEditText.length() == 0) {
                        mTitleEditText.setText(title);
                    }
                }
            });
            webView.loadUrl(text);
        } else if (!TextUtils.isEmpty(text)) {
            extractUrl(text);
        }
    }
}

From source file:org.projecthdata.hhub.ui.HDataWebOauthActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_PROGRESS);
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
    super.onCreate(savedInstanceState);

    webView = new WebView(this);
    setContentView(webView);/*w  w w. j a  v  a2 s.  co m*/
    this.ehrUrl = getIntent().getStringExtra(EXTRA_EHR_URL);

    activity = this;

    this.connectionRepository = getApplicationContext().getConnectionRepository();
    this.hDataConnectionFactory = getApplicationContext().getHDataConnectionFactory(ehrUrl);

    webView.setWebChromeClient(new WebChromeClient() {

        public void onProgressChanged(WebView view, int progress) {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);
            if (progress == 100) {
                activity.setTitle(R.string.app_name);
            }
        }
    });
    webView.setWebViewClient(new MyWebViewClient());

    // clear out any previously used credentials
    webView.clearCache(true);
    webView.clearFormData();
    webView.clearHistory();
    webView.getSettings().setSavePassword(false);
    webView.getSettings().setSaveFormData(false);
    CookieSyncManager.createInstance(this);
    CookieManager.getInstance().removeAllCookie();
}

From source file:com.owncloud.android.ui.activity.ExternalSiteWebView.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log_OC.v(TAG, "onCreate() start");

    Bundle extras = getIntent().getExtras();
    String title = extras.getString(EXTRA_TITLE);
    String url = extras.getString(EXTRA_URL);
    menuItemId = extras.getInt(EXTRA_MENU_ITEM_ID);
    showSidebar = extras.getBoolean(EXTRA_SHOW_SIDEBAR);

    // show progress
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.externalsite_webview);

    WebView webview = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = webview.getSettings();

    webview.setFocusable(true);//from   w  w w . j av  a 2  s  . co  m
    webview.setFocusableInTouchMode(true);
    webview.setClickable(true);

    // setup toolbar
    setupToolbar();

    // setup drawer
    setupDrawer(menuItemId);

    if (!showSidebar) {
        setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }

    getSupportActionBar().setTitle(title);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // enable zoom
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);

    // Non-responsive webs are zoomed out when loaded
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);

    // user agent
    webSettings.setUserAgentString(MainApp.getUserAgent());

    // no private data storing
    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);

    // disable local file access
    webSettings.setAllowFileAccess(false);

    // enable javascript
    webview.getSettings().setJavaScriptEnabled(true);

    final Activity activity = this;
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);

    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress * 1000);
        }
    });

    webview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, getString(R.string.webview_error) + ": " + description, Toast.LENGTH_SHORT)
                    .show();
        }
    });

    webview.loadUrl(url);
}

From source file:com.mummyding.app.leisure.ui.support.BaseWebViewActivity.java

private void initData() {
    data = getData();/*from   w  w w  .j av a2  s.  c  o m*/

    webView = (WebView) findViewById(R.id.webview);
    progressBar = (ProgressBar) findViewById(R.id.progressbar);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setSupportMultipleWindows(false);
    /*
     cache web page
     */

    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);

    /*  if(Settings.isNightMode) {
    webView.setBackgroundColor(ContextCompat.getColor(this, R.color.night_primary));
     }*/
    if (HttpUtil.isWIFI == false) {
        webView.getSettings()
                .setBlockNetworkImage(Settings.getInstance().getBoolean(Settings.NO_PIC_MODE, false));
    } else {
        // fix issue #13
        webView.getSettings().setBlockNetworkImage(false);
    }

    loadData();
    setProgressBarIndeterminateVisibility(true);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
        }

        /* @Override
         public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        view.loadUrl("file:///android_asset/error.html");
         }
                
         @Override
         public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        view.loadUrl("file:///android_asset/error.html");
         }*/

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (isLoading) {
                progressBar.incrementProgressBy(newProgress - progressBar.getProgress());
                if (newProgress > 25) {
                    isLoading = false;
                    progressBar.setVisibility(View.GONE);
                }
            }
        }
    });
}