4
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Main();

}
public void Main()
{
    _linearLayout = new LinearLayout(this);
    _webview = new WebView(this);
    _linearLayout.addView(_webview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    setContentView(_linearLayout);

    _webview.getSettings().setJavaScriptEnabled(true);
    _webview.getSettings().setPluginsEnabled(true);
    _webview.getSettings().setAllowFileAccess(true);

    _webview.setWebChromeClient(new WebChromeClient());
    _webview.addJavascriptInterface(this, "Question");
    _webview.loadData(GetHTML(), "text/html", "utf-8");


}

public String GetHTML()
{   
    String HTML = ""
        + "<HTML>"
        + "<HEAD>"
        + "<TITLE>Radio Button onClick Handler</TITLE>"
        + "<SCRIPT LANGUAGE=\"JavaScript\">"
        +"function function1(colors) {" 
        +"var col = (colors.options[colors.selectedIndex].value);"
        +" if (col) {"
        +"  document.bgColor = col;"

        +"   } "
        +"</script>"
        + "</HEAD>"
        + "<BODY>"
        +"<form>"
        +"<b> Hello </b>"
        //+"<select name=\"colors\" onChange=\"window.Question.function1(this);\">"
        +"<select name=\"colors\" onChange=\"window.Question.OnJsClick_SelectedItem(' string value');\">"
           +"<option value=\"white\" selected>White</option>"
           + "<option value=\"cyan\">Cyan</option>"
           + "<option value=\"ivory\">Ivory</option>"
           + "<option id=\"myO\" value=\"blue\">Blue</option>"

        +"</select>"
        +"</form>"
        + "</BODY>"
        + "</HTML>";

    return HTML;
}

public void OnJsClick_SelectedItem(final String str)
{
    mHandler.post(new Runnable()
    {
        //@Override
        public void run()
        {
            getValue(str);
        }
    });
}

public String getValue(String str)
{
    _webview.loadUrl("javascript:function1(colors)");
    Toast.makeText(this, "Under getValue " + str, Toast.LENGTH_SHORT).show();
    return str;

}
}

Please help me out.

1
  • 1
    you should accept answers to your questions if you've found them to be useful(See there is a tick there)and also use upvotes. It will help you get more answers. Commented Jun 15, 2011 at 11:48

2 Answers 2

5

You can store the code as a HTML page in assets folder and use the load URL method to the webview to show the webpage.

mWebView.loadUrl("file:///android_asset/index.html");

Edits according to the comment

Enabling JavaScript

 WebView webView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

here's some HTML and JavaScript that creates a toast message using the new interface when the user clicks a button

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

Add this in your Java code

public class JavaScriptInterface {
    Context mContext;

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

    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

That's fine I can load HTML Page from asset folder and load via loadURL. Here my problem is I want to get the selected value of combo box inside Toast.Suppose user select ivory color then my toast print ivory selected. Can u help me please?? @Rishabh
I am able to load HTML inside webview but I want to pass the selected value (combo box selected value written in javascript) to java code
try the editted code out it will enable integrating java code into javascript, HTML. And accept if it works
Just call a function in the javascript and pass the value to it .
2

You should use something like this:

<select name="colors"
    onChange="Question.OnJsClick_SelectedItem(this.options[this.selectedIndex].text)">

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.