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>
3
  • may i knw for wt parpose you have to use it?? Commented Jan 22, 2014 at 9:11
  • Double. stackoverflow.com/questions/7544671/… Commented Jan 22, 2014 at 9: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. Commented Jan 22, 2014 at 9:21

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

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
try it ,Can perform actions, but does not return a value. webview.loadUrl("javascript:gettext()");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.