Example usage for android.webkit WebView WebView

List of usage examples for android.webkit WebView WebView

Introduction

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

Prototype

public WebView(Context context) 

Source Link

Document

Constructs a new WebView with an Activity Context object.

Usage

From source file:cn.zy.ef.fragment.WebViewFragment.java

/**
 * Called to instantiate the view. Creates and returns the WebView.
 *///from w w  w . ja  v a2 s .com
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (mWebView != null) {
        mWebView.destroy();
    }
    mWebView = new WebView(getActivity());
    mWebView.setWebChromeClient(new WebChromeClient());
    //mWebView.addJavascriptInterface(new InJavaScriptLocalObj(), "androidphone");
    configureWebView(mWebView.getSettings());
    mIsWebViewAvailable = true;
    return mWebView;
}

From source file:com.barcamppenang2013.tabfragment.IndoorMapFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (mWebView != null) {
        mWebView.destroy();//from   w w w. j  a va2 s.  c o  m
    }
    mWebView = new WebView(getActivity());
    mWebView.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            }
            return false;
        }

    });
    mWebView.setWebViewClient(new InnerWebViewClient());
    String imageUrl = "file:///android_asset/qb_indoor_map.png";

    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.loadUrl(imageUrl);

    mIsWebViewAvailable = true;
    //      WebSettings settings = mWebView.getSettings();
    //      settings.setJavaScriptEnabled(true);
    return mWebView;
}

From source file:org.geometerplus.android.fbreader.network.auth.WebAuthorisationScreen.java

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    requestWindowFeature(Window.FEATURE_PROGRESS);
    SQLiteCookieDatabase.init(this);
    CookieSyncManager.createInstance(getApplicationContext());
    CookieManager.getInstance().removeAllCookie();
    final Intent intent = getIntent();
    final Uri data = intent.getData();
    if (data == null || data.getHost() == null) {
        finish();/*from   w ww .jav a 2 s.c  om*/
        return;
    }
    final String completeUrl = intent.getStringExtra(COMPLETE_URL_KEY);

    OrientationUtil.setOrientation(this, intent);
    final WebView view = new WebView(this);
    view.getSettings().setJavaScriptEnabled(true);

    view.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            setProgress(progress * 100);
        }
    });
    view.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            setTitle(url);
            if (url != null && url.startsWith(completeUrl)) {
                final HashMap<String, String> cookies = new HashMap<String, String>();
                final String cookieString = CookieManager.getInstance().getCookie(url);
                if (cookieString != null) {
                    // cookieString is a string like NAME=VALUE [; NAME=VALUE]
                    for (String pair : cookieString.split(";")) {
                        final String[] parts = pair.split("=", 2);
                        if (parts.length != 2) {
                            continue;
                        }
                        cookies.put(parts[0].trim(), parts[1].trim());
                    }
                }
                storeCookies(data.getHost(), cookies);
                WebAuthorisationScreen.this.setResult(RESULT_OK);
                finish();
            }
        }

        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                // hack for auth problem in android 2.1
                handler.proceed();
            } else {
                super.onReceivedSslError(view, handler, error);
            }
        }
    });
    setContentView(view);
    view.loadUrl(intent.getDataString());
}

From source file:com.barcamppenang2013.tabfragment.SponsorFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (mWebView != null) {
        mWebView.destroy();/*from   w w w.  j  a  va  2s .  co m*/
    }
    mWebView = new WebView(getActivity());
    mWebView.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            }
            return false;
        }

    });
    //      mWebView.setWebViewClient(new InnerWebViewClient());
    //        String imageUrl =  "file:///android_asset/info_sponsor.png";
    //      String imageUrl = "";
    mWebView.getSettings().setJavaScriptEnabled(true);

    //        mWebView.loadUrl(imageUrl);
    mWebView.loadData(readFromFile("sponsor.html"), "text/html", "utf-8");

    mIsWebViewAvailable = true;
    //      WebSettings settings = mWebView.getSettings();
    //      settings.setJavaScriptEnabled(true);
    return mWebView;
}

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

@SuppressLint("SetJavaScriptEnabled")
private WebView createWebView(final Context context) {
    WebView webView = new WebView(context);
    webView.setWebViewClient(createWebViewClient());
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setVisibility(View.VISIBLE);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(false);
    return webView;
}

From source file:ph.com.globe.connect.AuthenticationActivity.java

/**
 * On activity create process./*  w  w  w . j  av  a 2s .  c  o  m*/
 *
 * @param savedInstanceState instance state
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    // call base create
    super.onCreate(savedInstanceState);

    // initialize web view
    final WebView webview = new WebView(this);

    // set content view of the activity
    setContentView(webview);

    // get the app id from intent
    final String appId = getIntent().getStringExtra("app_id");
    // get the app secret from intent
    final String appSecret = getIntent().getStringExtra("app_secret");

    // set web view client
    webview.setWebViewClient(new WebViewClient() {
        /**
         * Let's catch all url changes.
         *
         * @param view current view
         * @param url current url
         * @return boolean
         */
        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // parse the uri
            final Uri uri = Uri.parse(url);

            // are we still on globe labs?
            if (uri.toString().indexOf(ROOT_URL) != 0) {
                // get the code
                String code = uri.getQueryParameter("code");

                // get access token request
                try {
                    getAccessToken(appId, appSecret, code);
                } catch (ApiException | HttpRequestException e) {
                    e.printStackTrace();
                }

                return false;
            }

            // load uri
            view.loadUrl(uri.toString());

            return false;
        }

        /**
         * Let's catch all url changes.
         *
         * @param view current view
         * @param request web resource request
         * @return boolean
         */
        @TargetApi(Build.VERSION_CODES.N)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            // parse the uri
            final Uri uri = Uri.parse(request.getUrl().toString());

            // are we still on globe labs?
            if (uri.toString().indexOf(ROOT_URL) != 0) {
                // get the code
                String code = uri.getQueryParameter("code");

                // get access token request
                try {
                    getAccessToken(appId, appSecret, code);
                } catch (ApiException | HttpRequestException e) {
                    e.printStackTrace();
                }

                return false;
            }

            // load uri
            view.loadUrl(uri.toString());

            return false;
        }
    });

    try {
        // set dialog url
        String DIALOG_URL = this.buildUrl(this.DIALOG_URL, appId);

        // load the url
        webview.loadUrl(DIALOG_URL);
    } catch (ApiException e) {
        e.printStackTrace();
    }
}

From source file:org.mariotaku.twidere.fragment.WebViewFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        final Bundle savedInstanceState) {
    return mWebView = new WebView(getActivity());
}

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);
    }/*  ww w . ja  v  a  2  s.  c o  m*/
    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:org.septa.android.app.utilities.WebViewFragment.java

/**
 * Called to instantiate the view. Creates and returns the WebView.
 *//*from   w w w . j  av a 2 s.c o  m*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (mWebView != null) {
        mWebView.destroy();
    }
    mLayout = new FrameLayout(getActivity());
    mWebView = new WebView(getActivity());
    mIsWebViewAvailable = true;

    mWebView.getSettings().setJavaScriptEnabled(true);

    /** check if we have already set the Url to load, if yes, load it */
    if (urlToLoad != null && !urlToLoad.trim().equals("")) {
        mWebView.loadUrl(urlToLoad);
    }

    mLayout.addView(mWebView, LayoutParams.MATCH_PARENT);

    return mLayout;
}

From source file:com.akingyin.librarys.widgets.BrowserLayout.java

private void init(Context context) {
    mContext = context;//w w w.  j a  v  a 2  s .  c om
    setOrientation(VERTICAL);

    mProgressBar = (ProgressBar) LayoutInflater.from(context).inflate(R.layout.progress_horizontal, null);
    mProgressBar.setMax(100);
    mProgressBar.setProgress(0);
    addView(mProgressBar, LayoutParams.MATCH_PARENT, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
            mBarHeight, getResources().getDisplayMetrics()));

    mWebView = new WebView(context);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    mWebView.getSettings().setBuiltInZoomControls(false);
    mWebView.getSettings().setSupportMultipleWindows(true);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setSupportZoom(false);
    mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setLoadsImagesAutomatically(true);

    LayoutParams lps = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1);
    addView(mWebView, lps);

    mWebView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (newProgress == 100) {
                mProgressBar.setVisibility(View.GONE);
            } else {
                mProgressBar.setVisibility(View.VISIBLE);
                mProgressBar.setProgress(newProgress);
            }
        }
    });

    mWebView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mLoadUrl = url;
        }
    });

    mBrowserControllerView = LayoutInflater.from(context).inflate(R.layout.browser_controller, null);
    mGoBackBtn = (ImageButton) mBrowserControllerView.findViewById(R.id.browser_controller_back);
    mGoForwardBtn = (ImageButton) mBrowserControllerView.findViewById(R.id.browser_controller_forward);
    mGoBrowserBtn = (ImageButton) mBrowserControllerView.findViewById(R.id.browser_controller_go);
    mRefreshBtn = (ImageButton) mBrowserControllerView.findViewById(R.id.browser_controller_refresh);

    mGoBackBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (canGoBack()) {
                goBack();
            }
        }
    });

    mGoForwardBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (canGoForward()) {
                goForward();
            }
        }
    });

    mRefreshBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            loadUrl(mLoadUrl);
        }
    });

    mGoBrowserBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!TextUtils.isEmpty(mLoadUrl)) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(mLoadUrl));
                mContext.startActivity(intent);
            }
        }
    });

    addView(mBrowserControllerView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}