7

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.

HotFudgeSunday
  • 1,403
  • 2
  • 23
  • 29

3 Answers3

23

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.

Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
  • 3
    Thanks no.good.at.coding. Although that should be my name. ;-) – HotFudgeSunday Feb 25 '11 at 10:53
  • One thing not mentioned in the tutorial are the imports: import android.webkit.WebChromeClient; import android.webkit.WebView; . You can hover over the code underlined in red and you'll get some options to fix it. The first one will be the imports. – HotFudgeSunday Feb 25 '11 at 11:04
  • obrigado o que faltou aqui foi isso} webView.setWebChromeClient(new WebChromeClient()); – BREI May 20 '23 at 22:19
2

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);
Community
  • 1
  • 1
computingfreak
  • 4,939
  • 1
  • 34
  • 51
-2

just add

import android.webkit.WebChromeClient;
import android.webkit.WebView;

in YourApp.java

Ori Lentz
  • 3,668
  • 6
  • 22
  • 28