Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: can't get javascript to work on WebView even with setJavaScriptEnabled(true)

I'm a complete noob to Android and this is just a simple test. Based it on this tutorial.

Here goes the HelloWebApp.java

package com.company.something;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class HelloWebApp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("file:///android_asset/www/index.html");
    }
}

And this is from res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<WebView  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/webView"
    />
</LinearLayout>

Plus this is all I changed on the Manifest:

<activity android:name=".HelloWebApp"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
android:screenOrientation="landscape">

As for the javascript, I've tried everything. Complicated, simple, inside the body at the bottom, in a button, on the head. Nothing works. The html works fine.

Thanks in advance for any help.

like image 380
HotFudgeSunday Avatar asked Feb 24 '11 21:02

HotFudgeSunday


People also ask

How do I enable JavaScript on Android WebView?

Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

Is Android WebView deprecated?

It was deprecated in API level 21.


2 Answers

You missed the part in the tutorial where he adds

webView.setWebChromeClient(new WebChromeClient());

right after adding

webView.getSettings().setJavaScriptEnabled(true);

The JavaDoc for this method says:

Sets the chrome handler. This is an implementation of WebChromeClient for use in handling Javascript dialogs, favicons, titles, and the progress. This will replace the current handler.

like image 187
no.good.at.coding Avatar answered Nov 14 '22 20:11

no.good.at.coding


As discussed in https://stackoverflow.com/a/7561674/1818089,

along with

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());

you need to enable DOM storage

WebSettings webSettings = webView.getSettings();
webSettings.setDomStorageEnabled(true);
like image 36
computingfreak Avatar answered Nov 14 '22 21:11

computingfreak