Code example for WebView: Displaying web pages

Methods: addJavascriptInterfacegetSettingsloadUrl, requestFocusFromTouch, setHorizontalScrollBarEnabled, setVerticalScrollBarEnabled

0
         
    } 
     
    private void loadMain() { 
        // Enable JavaScript 
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
         
        //hidden scroll bar. 
        myWebView.setHorizontalScrollBarEnabled(false);
        myWebView.setVerticalScrollBarEnabled(false);
         
        //add key board. 
        myWebView.requestFocusFromTouch();
         
              
        //enable javaScript alert. 
        final Context myApp = this;
        myWebView.setWebChromeClient(new WebChromeClient(){
             
            // Handle javaScript alert in webview. 
            @Override 
            public boolean onJsAlert(WebView view, String url, String message,
                    final android.webkit.JsResult result) {
                new AlertDialog.Builder(myApp)
                    .setTitle("JavaScript Dialog")
                    .setMessage(message)
                    .setPositiveButton(android.R.string.ok, 
                            new AlertDialog.OnClickListener(){
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                result.confirm();
                            } 
                    }) 
                    .setCancelable(false)
                    .create()
                    .show();
                 
                return true; 
            } 
             
            /* 
            public void openFileChooser(ValueCallback<Uri> uploadMsg) { 
                Toast.makeText(getBaseContext(), "openfile", Toast.LENGTH_LONG).show(); 
            } 
            */ 
             
        }); 
        
        jsInterface = new JavaScriptInterface(this);
        myWebView.addJavascriptInterface(jsInterface, "Android");
        myWebView.addJavascriptInterface(this, "Activity");
         
        // Load a web page 
//        myWebView.loadUrl("file:///android_asset/index.html?id=2"); 
        //myWebView.loadUrl("file:///android_asset/test_xiaoyong.html"); 
        myWebView.loadUrl("file:///android_asset/welcomePage.html");
        //myWebView.loadUrl("file:///android_asset/activeItem.html?id=1&time=" + System.currentTimeMillis()); 
         
        //////-----------For bottom panel-----------------------------///////// 
         
        bPanel = (WebView) findViewById(R.id.bPanel);
        bPanel.loadUrl("file:///android_asset/bottomPanel.html");
        WebSettings bPanelSettings = bPanel.getSettings();
        bPanelSettings.setJavaScriptEnabled(true);
        //hidden scroll bar. 
        bPanel.setHorizontalScrollBarEnabled(false);
        bPanel.setVerticalScrollBarEnabled(false);
         
        //bind top panel communication interface. 
        bPanel.addJavascriptInterface(new TopPanelCommunicationInterface(handler, myWebView), "TopInterface");
        myWebView.addJavascriptInterface(new TopPanelCommunicationInterface(handler, bPanel), "BotInterface");
         
        //////-----------bottom panel END-----------------------------///////// 
    } 
     
    private void loadReview(long id, long time) {
        // full screen 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
         
        // Enable JavaScript 
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
         
        //hidden scroll bar. 
        myWebView.setHorizontalScrollBarEnabled(false);
        myWebView.setVerticalScrollBarEnabled(true);
         
        //add key board. 
        myWebView.requestFocusFromTouch();
         
        jsInterface = new JavaScriptInterface(this);
        myWebView.addJavascriptInterface(jsInterface, "Android");
        myWebView.addJavascriptInterface(this, "Activity");
 
        String sId = String.valueOf(id);
        String sTime = String.valueOf(time);
        myWebView.loadUrl("file:///android_asset/activeItem.html?id=" + sId + "&time=" + sTime);
        //myWebView.loadUrl("file:///android_asset/activeItem.html"); 
         
        bPanel = (WebView) findViewById(R.id.bPanel);
        bPanel.setVisibility(View.INVISIBLE);
    }