0

In this code I return a string. I want to store this string in android activity. How can I call JavaScript function and get return value from android activity?

 <script type="text/javascript">
        function gettext(){
            return 'http://192.168.1.11/bmahtml5/images/specs_larg_2.png';
        };
        function button_clicked(){
            window.location='ios:nativecall';
        };
 </script>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
ishu
  • 1,322
  • 1
  • 11
  • 15
  • may i knw for wt parpose you have to use it?? – Digvesh Patel Jan 22 '14 at 09:11
  • Double. http://stackoverflow.com/questions/7544671/how-to-call-javascript-from-android – Tapa Save Jan 22 '14 at 09:16
  • actually i load html page and when i click on some portion when i have to call thi gettext function which returns link of image and i store this image in particular variable and show image in image view. – ishu Jan 22 '14 at 09:21

1 Answers1

0

here is a simple :

WebAppInterface.java

public class WebAppInterface
 {
        Context mContext;

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

        public void storeText(String text)
        {
            //TODO  save text
            Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
        }
    }

WebView webview=(WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new WebAppInterface(this), "Android");

Now you can call this method in html ,see it

<script type="text/javascript">

    function saveFunction(text) 
    {
        Android.storeText(text);
    }
</script>
hoafer
  • 106
  • 4
  • hi thnks i tried thi code in your script function android.stroretext function callls and in my script function just return string value how can i get this string – ishu Jan 22 '14 at 09:36
  • try it ,Can perform actions, but does not return a value. webview.loadUrl("javascript:gettext()"); – hoafer Jan 23 '14 at 01:49