Example usage for android.webkit WebViewClient WebViewClient

List of usage examples for android.webkit WebViewClient WebViewClient

Introduction

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

Prototype

WebViewClient

Source Link

Usage

From source file:com.yoavst.quickapps.news.LoginActivity.java

private WebViewClient createWebViewClient() {
    return new WebViewClient() {
        @Override/*w ww. j  a v a  2s.c  o  m*/
        public void onPageStarted(WebView view, String url, Bitmap fav) {
            mProgressDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            mProgressDialog.dismiss();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith(REDIRECT_URI_LOCAL) || url.startsWith(REDIRECT_URN)) {
                mProgressDialog.show();
                handleUrl(url);
                return true;
            }
            mProgressDialog.dismiss();
            return false;
        }
    };
}

From source file:com.amazonaws.devicefarm.android.referenceapp.Fragments.WebViewFragment.java

/**
 * Generates a WebView client with custom logic for calabash tests
 *
 * @return WebViewClient//from w  w  w . ja  v  a2s  . c o  m
 */
private WebViewClient generateWebViewClient() {
    return new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.i("WebView", url);
            if (!isError) {
                try {
                    webView.setContentDescription((new URL(url)).getHost());
                } catch (MalformedURLException e) {
                    Log.e("WebView", "Malformed url error");
                }
            } else {
                webView.setContentDescription(getString(R.string.webview_fail));
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            isError = true;
        }
    };
}

From source file:com.near.chimerarevo.fragments.WebFragment.java

@SuppressLint("SetJavaScriptEnabled")
@Override//from   w  w  w .  j  a  v  a  2  s  .co m
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ((BaseActivity) getActivity()).getToolbar().setTitle(getResources().getString(R.string.app_name));

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.setHorizontalScrollBarEnabled(true);
    mWebView.setHorizontalScrollbarOverlay(true);
    mWebView.setVerticalScrollBarEnabled(true);
    mWebView.setVerticalScrollbarOverlay(true);

    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost()
                    .contains(Uri.parse(getArguments().getString(Constants.KEY_URL)).getHost()))
                return false;

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mProgressContainer.setVisibility(View.VISIBLE);
            mWebContainer.setVisibility(View.GONE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mProgressContainer.setVisibility(View.GONE);
            mWebContainer.setVisibility(View.VISIBLE);
        }

    });

    if (getArguments().containsKey(Constants.KEY_URL))
        mWebView.loadUrl(getArguments().getString(Constants.KEY_URL));
}

From source file:com.google.blockly.android.codegen.CodeGeneratorService.java

@Override
public void onCreate() {
    mHandler = new Handler();
    mWebview = new WebView(this);
    mWebview.getSettings().setJavaScriptEnabled(true);
    mWebview.setWebChromeClient(new WebChromeClient());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }//from   ww  w . j  a v a  2 s  . c  om
    mWebview.addJavascriptInterface(new BlocklyJavascriptInterface(), "BlocklyJavascriptInterface");

    mWebview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            synchronized (this) {
                mReady = true;
            }
            handleRequest();
        }
    });
    mWebview.loadUrl(BLOCKLY_COMPILER_PAGE);
}

From source file:com.example.nq.journalism_master.innerbrowser.InnerBrowserFragment.java

@Nullable
@Override/* www.  j  a  va  2  s  . c  o m*/
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_inner_browser, container, false);

    initViews(view);
    initWebViewSettings(webView);

    // if not set this, click the back arrow will call nothing
    setHasOptionsMenu(true);

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            changeToolbarAndProgressBar(newProgress);
        }
    });

    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedError(WebView view1, WebResourceRequest request, WebResourceError error) {

            webView.setVisibility(View.GONE);
            imageView.setVisibility(View.VISIBLE);
            textView.setVisibility(View.VISIBLE);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (NetworkState.networkConnected(getContext())) {
                        webView.loadUrl(url);
                        webView.setVisibility(View.VISIBLE);
                        imageView.setVisibility(View.GONE);
                        textView.setVisibility(View.GONE);
                    }
                }
            });

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
        }
    });

    // WebView?html?
    webView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                    webView.goBack();
                    return true;
                }
            }
            return false;
        }
    });

    webView.loadUrl(url);

    return view;
}

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

@SuppressLint("SetJavaScriptEnabled")
@Override/*  w  w w . ja  v  a 2 s .  co 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:org.smartrobot.android.web.RobotWebView.java

private void init() {
    getSettings().setJavaScriptEnabled(true);
    addJavascriptInterface(mSmartRobotHandler, "SmartRobotHandler");
    setWebViewClient(new WebViewClient() {
        @Override/*  ww w. j av a 2  s  .  c o  m*/
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            mPageLoaded = false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (mInitialized)
                initialize();
            if (mActivated)
                loadUrl("javascript:onActivated()");
            mPageLoaded = true;
        }
    });
}

From source file:com.btmura.android.reddit.app.LinkFragment.java

@SuppressLint("SetJavaScriptEnabled")
private void setupWebView(WebView webView) {
    WebSettings settings = webView.getSettings();
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    settings.setDomStorageEnabled(true);
    settings.setJavaScriptEnabled(true);
    settings.setLoadWithOverviewMode(true);
    settings.setSupportZoom(true);/*w  ww  .  j  a va  2  s.c  o  m*/
    settings.setPluginState(PluginState.ON_DEMAND);
    settings.setUseWideViewPort(true);

    webView.setOnLongClickListener(this);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (progress != null) {
                progress.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (progress != null) {
                progress.setVisibility(View.GONE);
            }
        }
    });

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (progress != null) {
                progress.setProgress(newProgress);
            }
        }
    });
}

From source file:org.golang.app.WViewActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Fixed Portrait orientation
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    /*this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);*/

    setContentView(R.layout.webview);//from  ww w  . ja v a2 s.c o  m
    WebView webView = (WebView) findViewById(R.id.webView1);

    initWebView(webView);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.d("JavaGoWV: ", url);

            final Pattern p = Pattern.compile("dcoinKey&password=(.*)$");
            final Matcher m = p.matcher(url);
            if (m.find()) {
                try {
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                //File root = android.os.Environment.getExternalStorageDirectory();
                                File dir = Environment
                                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                                Log.d("JavaGoWV", "dir " + dir);

                                URL keyUrl = new URL(
                                        "http://127.0.0.1:8089/ajax?controllerName=dcoinKey&first=1"); //you can write here any link
                                //URL keyUrl = new URL("http://yandex.ru/"); //you can write here any link
                                File file = new File(dir, "dcoin-key.png");

                                long startTime = System.currentTimeMillis();
                                Log.d("JavaGoWV", "download begining");
                                Log.d("JavaGoWV", "download keyUrl:" + keyUrl);

                                /* Open a connection to that URL. */
                                URLConnection ucon = keyUrl.openConnection();

                                Log.d("JavaGoWV", "0");
                                /*
                                * Define InputStreams to read from the URLConnection.
                                */
                                InputStream is = ucon.getInputStream();

                                Log.d("JavaGoWV", "01");

                                BufferedInputStream bis = new BufferedInputStream(is);

                                Log.d("JavaGoWV", "1");
                                /*
                                * Read bytes to the Buffer until there is nothing more to read(-1).
                                */
                                ByteArrayBuffer baf = new ByteArrayBuffer(5000);
                                int current = 0;
                                while ((current = bis.read()) != -1) {
                                    baf.append((byte) current);
                                }

                                Log.d("JavaGoWV", "2");
                                /* Convert the Bytes read to a String. */
                                FileOutputStream fos = new FileOutputStream(file);
                                fos.write(baf.toByteArray());
                                fos.flush();
                                fos.close();

                                Log.d("JavaGoWV", "3");
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                    Uri contentUri = Uri.fromFile(file);
                                    mediaScanIntent.setData(contentUri);
                                    WViewActivity.this.sendBroadcast(mediaScanIntent);
                                } else {
                                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                                            Uri.parse("file://" + Environment.getExternalStorageDirectory())));
                                }

                                Log.d("JavaGoWV", "4");
                            } catch (Exception e) {
                                Log.e("JavaGoWV error 0", e.toString());
                                e.printStackTrace();
                            }
                        }
                    });
                    thread.start();

                } catch (Exception e) {
                    Log.e("JavaGoWV error", e.toString());
                    e.printStackTrace();
                }
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Log.e("JavaGoWV", "shouldOverrideUrlLoading " + url);

            if (url.endsWith(".mp4")) {
                Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(in);
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.d("JavaGoWV",
                    "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
        }
    });

    SystemClock.sleep(1500);
    //if (MyService.DcoinStarted(8089)) {
    webView.loadUrl("http://localhost:8089/");
    //}

}

From source file:com.usabusi.newsreader.ArticleFragment.java

/**
 * Sets up the UI. It consists if a single WebView.
 *//*from   w  w  w . j a  v  a  2  s.  co  m*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mWebView = new WebView(getActivity());
    //http://stackoverflow.com/questions/17259537/load-webview-from-cache
    WebSettings webSettings = mWebView.getSettings();
    // Enable JavaScript
    webSettings.setJavaScriptEnabled(true); // enable javascript
    mWebView.setHorizontalScrollBarEnabled(false);
    webSettings.setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
    webSettings.setAppCachePath(getActivity().getApplicationContext().getCacheDir().getAbsolutePath());
    webSettings.setAllowFileAccess(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default
    //http://stackoverflow.com/questions/25161720/url-opened-in-browser-instead-of-web-view
    mWebView.setVisibility(View.VISIBLE);
    webSettings.setPluginState(WebSettings.PluginState.ON);
    webSettings.setBuiltInZoomControls(true);

    //final Activity activity = this;
    // Make WebClient
    mWebView.setWebViewClient(new WebViewClient() {
        // Trace Errors
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(getActivity().getApplicationContext(), description, Toast.LENGTH_SHORT).show();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //http://developer.android.com/guide/webapps/webview.html#HandlingNavigation
            //                view.loadUrl(url);
            //                return true;
            return false;
        }
    });

    loadWebView();
    return mWebView;
}