How to run javascript in Android webview

3

I have Activity with the following layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".WebviewActivity"
          android:orientation="vertical">

<WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</FrameLayout>

Within my Activity I'm opening a particular url in webview . This url has some javascript.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview_activity);
    Webview webView = (WebView) findViewById(R.id.webview);
    webView.loadUrl("http://paginacomjavascript.com");
}

For some reason Webview is not working.

    
asked by anonymous 20.03.2014 / 16:17

1 answer

4

By default in WebView javascript is disabled.

For enabled you need to change the WebView settings as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview_activity);
    Webview webView = (WebView) findViewById(R.id.webview);

    WebSettings webSettings = webView.getSettings(); //<<-- Retorna configurações
    webSettings.setJavaScriptEnabled(true);          //<<-- Altera aqui !!!

    webView.loadUrl("http://paginacomjavascript.com");
}
    
20.03.2014 / 16:17