Code example for WebView

Methods: addJavascriptInterfacegetSettingssetWebChromeClientsetWebViewClient, setVisibility

0
 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Create a WebView and make layout. 
        mWebView = new WebView(this);
        FrameLayout layout = new FrameLayout(this);
        addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.addView(mWebView, new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.NO_GRAVITY));
        // Basic settings of WebView. 
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setSupportZoom(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginsEnabled(true);
        //webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); 
        // Set a dummy WebViewClient (which enables loading a new page in own WebView). 
        mWebView.setWebViewClient(new WebViewClient(){});
        // Add a progress bar. 
        mProgress = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
        layout.addView(mProgress, new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, 5));
        mProgress.setMax(100);
        mProgress.setVisibility(View.GONE);
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress < 100) {
                    mProgress.setVisibility(View.VISIBLE);
                    mProgress.setProgress(progress);
                } else { 
                    mProgress.setVisibility(View.GONE);
                } 
            } 
        }); 
        // Create a JavaScript interface and bind the WebView to it. 
        mJSInterface = new JSInterface();
        mWebView.addJavascriptInterface(mJSInterface, "UnityInterface");
        // Start in invisible state. 
        mWebView.setVisibility(View.GONE);
    } 
 
    public void updateWebView(final String lastRequestedUrl, final boolean loadRequest, final boolean visibility, final int leftMargin, final int topMargin, final int rightMargin, final int bottomMargin) {
        // Process load requests. 
        if (lastRequestedUrl != null && (loadRequest || !mInitialLoad)) { 
            runOnUiThread(new Runnable() {