How to implement WebView with JavaScript enabled? [duplicate]

-1

I have a blog in done in wordpress, and I would like to call this blog in a WebView component, how to implement the WebView component with the JavaScript enabled?

I have this code:

webView = (WebView) findViewById(R.id.checkbox_webview);
webView.loadUrl("file:///android_asset/index.html");

How to call an external page?

    
asked by anonymous 06.06.2016 / 19:13

2 answers

4
//Habilitar Javascript
 webView.getSettings().setJavaScriptEnabled(true);
//Carregar página
webView.loadUrl(“http://pt.stackoverflow.com/questions/133554/como-implementar-webview-com-javascript-habilitado")
    
06.06.2016 / 19:47
3

Paul,

Leaving your generic idea with a possibility of reuse, create a class called WebInterface and you can trigger an Intent whenever you need to load a WebView, with javascript enabled, error correction if SSL is not loaded and mainly by treating the back button to apply to the web context and not to the application.

Follows:

    public class WebInterface extends Activity {

    private WebView webView;
    private static final String TAG = "WebInterface";
    private ProgressDialog progressBar;
    private static String URL;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_web_interface);

        if(this.getIntent().hasExtra("url")){

            //Obtendo o URL.
            URL = this.getIntent().getStringExtra("url");

            this.webView = (WebView)findViewById(R.id.webView);
            WebSettings settings = webView.getSettings();
            settings.setJavaScriptEnabled(true);
            webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

            final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

            progressBar = ProgressDialog.show(this, "Aguarde", "Carregando ...");

            webView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Log.i(TAG, "Processando acesso...");
                    view.loadUrl(url);
                    return true;
                }

                public void onPageFinished(WebView view, String url) {
                    Log.i(TAG, "Terminou de carregar a URL: " + url);
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }

                @Override
                public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                    handler.proceed();
                }


            });
            webView.loadUrl(URL);
        }else{
            this.finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
<activity
        android:name=".modules.web.WebInterface"
        android:windowSoftInputMode="stateAlwaysHidden" />
    
06.06.2016 / 20:40