1

I have used JavaScript code to android using this one THIS DOCUMENT but i'am not getting String value why i am facing this problem ,any one have idea on this one, help me.

This is my code

 webview = (WebView) findViewById(R.id.webView);             
    webview.getSettings().setJavaScriptEnabled(true);   

    String html = " <input type='button' value='Say hello' onClick='showAndroidToast('Hello Android!')' />" +
            "<script type='text/javascript'>   function showAndroidToast(toast) { Android.showToast(toast); }</script>";     

    webview.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);    
    webview.addJavascriptInterface(new WebAppInterface(MainActivity.this), "Android");  

And WebAppInterface class like this

public class WebAppInterface {
Context mContext;

/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
    mContext = c;
}

/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
    Log.i(" ", " " + toast);
    Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show();

}
}

1 Answer 1

2

This part does not seem very viable:

'showAndroidToast('Hello Android!')'

Your quotes will not be closed correctly. Use escaped double quotes instead for the outer:

\"showAndroidToast('Hello Android!')\"
Sign up to request clarification or add additional context in comments.

2 Comments

in that tutorial i will see this one Caution: If you've set your targetSdkVersion to 17 or higher, is it possible only 17 or highet what about lower versions
@NagarjunaReddy This is basically a subject of new question. However: you can add javascript interfaces for API 8 and above. However it will not be using the annotation -it will go to ordinary class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.