How to run javascript in Android webview

Asked

Viewed 1,494 times

3

I have a 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>

Inside my Activity am opening a certain url on 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 the Webview not working.

1 answer

6


By default in Webview javascript is disabled.

For enabled you need to change 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");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.