Usage of WebView.addJavascriptInterface()

by Anton Pirker » Mon, 12 Oct 2009 21:53:08 GMT

Sponsored Links
 Hey List!

I have an Application that displays a Website in an WebView.  This is 
working quite good.
Now i want give the Website access to a Class of my Android Project with
WebView.addJavascriptInterface() [1]

This is my Java Code (in an Activity)

public void onCreate(Bundle savedInstanceState) {
    FileUtil fu = new FileUtil();

    WebView appView = (WebView) findViewById(;
    appView.addJavascriptInterface(fu, "FileUtil");

// The FileUtils class is a very basic test-class:
public class FileUtil {
    public FileUtils() { }

    public int read() {
       return 99;

And now i have a index.html accessable via the url <someUrl>
In this html page i try to call but nothing happens.
Then I displayed all members of the window object and my "FileUtil" is 

Have i forgot anything? How can i make my Java methods accessable from 

Thanks in advance!
Anton, Freelance Android Developer

[1] #addJavascriptInterface(java.lang.Object,%20java.lang.String)


Usage of WebView.addJavascriptInterface()

by Mark Murphy » Mon, 12 Oct 2009 22:10:08 GMT


Dump the "window." -- just reference FileUtil directly.

It's not supposed to be there. You do not get an actual Javascript
object from addJavascriptInterface().

It is a bit more like you get a new Javascript keyword -- you can write
"", and the Javascript interpreter will interpret it, but
there is no FileUtil object.

Mark Murphy (a Commons Guy)  | 

Android App Developer Books: 


Sponsored Links

Usage of WebView.addJavascriptInterface()

by Anton Pirker » Mon, 12 Oct 2009 23:11:12 GMT

 Hey Mark!

Ah, i see! That's why it is called addJavascriptINTERFACE() and not 
addJavascriptObject ;)

I am trying to create a file on the sd card from the website and i was 
testing on my phone that was connected to the notebook with an usb cable 
and the usb sharing was activated. and it seems if all this is true i 
cannot write to the sd-card! took my quite a while to find out about that.

Thanks for your hints!


Other Threads

1. Some problems i have encountered and would like fixes for:

I just got the G1 about 3 days ago, and i must say I'm extremely
impressed but i have encounter some problems that I believe would
seriously improve Android .

My downloads in market
-I have downloaded lots of apps to try out and i find my download list
in the market is over populated with old apps i tried and never used.
An option to delete old downloads from ur list should be added.

Lag When Quitting Browser
-When I click the home key from the browser, im brought to the home
screen but icons lags and dont load for an avg of 10-20 seconds.
- From my memory I have only really encountered this with the web

Text Times Wrong
-Im using this phone in Canada on Rogers Network and my text (sms)
messages do not thread properly. The times are totally out of whack.
-I believe we need to add an option for messages to either use time
stamp data or use the "time" the phone actually received the message.

Quitting an app (task manager)
-a task managed should be implemented
-Some apps i might not use for a while, and over that time I will open
6 other apps but i still want that specific app to continue to run.
-A Prime example is "NetCounter" i might not actively open the app but
i need it to run 24/7 so i can track my data usage.
-Not all apps have a QUIT option.

If there are fixes for the problems i have posted, it would be great
to receive as much information as possible but in general i hope
android develops fixes for the following issues. .


2. Possible bug in TableLayout

I think that there is possible bug in TableLayout SDK 1.0 rc2 when
constructing table from code. It seams that the weight  or height
attribute is not respected when rendering the layout.

The XML view creates a table with row at the top and row at the bottom
of the screen  which is what supposed to happen, so I created this
same table via code but the results are not what were expected. Maybe
I am missing something but it seams OK, all LayoutParams  are this

Sample Code
private View buildTableViewFromSource(){
                TableRow.LayoutParams pTable=new TableRow.LayoutParams

                TableLayout table = new TableLayout(context);

                TableRow rowTop = new TableRow(context);

                TableRow.LayoutParams pRowTop=new TableRow.LayoutParams


                TextView txt=new TextView(context);
                txt.setText("Top Content");

                rowTop.addView(txt, pRowTop);

                TableRow rowBottom=new TableRow(context);

                TextView txtBottom=new TextView(context);
                txtBottom.setText("Bottom Content");

                TableRow.LayoutParams pRowBottom=new TableRow.LayoutParams


                table.addView(rowTop, pRowTop);
                table.addView(rowBottom, pRowBottom);

                return table;

        private View buildTableViewFromXML(){
                LayoutInflater inflater=(LayoutInflater) getSystemService
                LinearLayout layout=(LinearLayout) 
                TableLayout table=(TableLayout) layout.findViewById

                TableRow rowContent=(TableRow)table.findViewById(;

                TextView txt=new TextView(context);
                txt.setText("Top Content");

                TableRow rowTabs=(TableRow)table.findViewById(;

                TextView txtBottom=new TextView(context);
                txtBottom.setText("Bottom Content");

                return table;

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="

        <TableRow android:id="@+id/rowTop"
                android:layout_width="wrap_content" android:layout_weight="1"


        <TableRow android:id="@+id/rowBottom"
                android:layout_height="wrap_content" android:layout_weight="0">
                        <TextView android:id="@+id/TextView01"
android:layout_height="wrap_content" android:text="HEllo"></TextView>


Any suggestions , if anyone else can confirm this I will post this as
a bug.


3. missing partner_setup in

4. Can MediaPlayer play .wav fomat audio?

5. viewable area

6. Android Emulator, Working altogether with ARM and DSP ?

7. 'cc1plus': execvp: No such file or directory