Pankaj Kharche
Pankaj Kharche

Reputation: 1329

Can I pass Particular Values from html page to My Activity?

I have one query. Can I pass Value from my html page to My Activity file.

html file located in assets/www folder and Activity file located in src/package_name

Upvotes: 2

Views: 1222

Answers (2)

user744881
user744881

Reputation:

You need to use JavaScriptInterface. In your web view add this Interface.

  1. Make JavaScriptInterface class like (Here you can use any name for your class)

     public class JavaScriptInterface {
         Context mContext;
    
         /** Instantiate the interface and set the context */
         JavaScriptInterface(Context context) {
             mContext = context;
         }
    
         /** Get passed value from the web page here */
         public void showMyValue(String passedValue) {
             android.util.Log.i("TAG", "I Got this value:" + passedValue);
         }
     }
    
  2. Add this interface in your webview like

     WebView webView = (WebView) findViewById(R.id.webview);
     webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
    
  3. Now in your webpage, call this method to pass your value and do something need full with that value

     <input type="button" value="ClickMe" onClick="passValueToAndroid('Hello Android!')" />
     <script type="text/javascript">
     function passValueToAndroid(yourPassingValue) {
          Android.showMyValue(yourPassingValue);
      }
     </script>
    

Upvotes: 7

user756706
user756706

Reputation:

Yes you can pass any variable from html to activity.

You need to create JavaScript Interface to interact between html to activity,

Refer this link for implementation detail

http://developer.android.com/guide/webapps/webview.html

Upvotes: 1

Related Questions