title: Xamarin - Call Android function with JavaScript
body: <p>I've been trying to access a function in Android from my JavaScript. It works perfectly in Java, but somehow it won't work in C#.</p>
  
  <p>EDIT: The index.html does open correctly in C#, but the console logs "[chromium] [INFO:CONSOLE(18)] "Uncaught TypeError: Object [object Object] has no method 'makeToast'", source: file:///android_asset/www/index.html?message= (18)
  [WebViewCallback] No application can handle file:///android_asset/www/index.html?message=fgg&amp;length=on"</p>
  
  <p>This is my activity:</p>
  
  <pre><code>namespace WebappTest
  {
  [Activity (Label = "WebappTest", MainLauncher = true, Icon = "@drawable/icon")]
  public class MainActivity : Activity
  {
  
      protected override void OnCreate (Bundle bundle)
      {
          base.OnCreate (bundle);
  
          // Set our view from the "main" layout resource
          SetContentView (Resource.Layout.Main);
  
          WebView webView = FindViewById&lt;WebView&gt; (Resource.Id.webView1);
          webView.LoadUrl("file:///android_asset/www/index.html");
  
          webView.Settings.JavaScriptEnabled = true;
          webView.AddJavascriptInterface(new WebViewJavaScriptInterface(this), "app");
      }
  }
  
  
  /*
   * JavaScript Interface. Web code can access methods in here 
   * (as long as they have the @JavascriptInterface annotation)
   */
  public class WebViewJavaScriptInterface : Java.Lang.Object{
  
      private Context context;
  
      /*
       * Need a reference to the context in order to sent a post message
       */
      public WebViewJavaScriptInterface(Context context){
          this.context = context;
      }
  
      /* 
       * This method can be called from Android. @JavascriptInterface 
       * required after SDK version 17. 
       */
      [JavascriptInterface]
      public void makeToast(String message, bool lengthLong){
          Toast.MakeText (context, message, (lengthLong ? ToastLength.Long : ToastLength.Short)).Show ();
      }
  }
  
  }
  </code></pre>
  
  <p>and this is my index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;JavaScript View&lt;/title&gt;
  
  &lt;script type="text/javascript"&gt;
  
      function showToast(){
          var message = document.getElementById("message").value;
          var long = document.getElementById("length").checked;
  
          /* 
              Call the 'makeToast' method in the Java code. 
              'app' is specified in MainActivity.java when 
              adding the JavaScript interface. 
           */
          app.makeToast(message, long);
          return false;
      }
  
      /* 
          Call the 'showToast' method when the form gets 
          submitted (by pressing button or return key on keyboard). 
       */
      window.onload = function(){
          var form = document.getElementById("form");
          form.onsubmit = showToast;
      }
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;form id="form"&gt;
  Message: &lt;input id="message" name="message" type="text"/&gt;&lt;br /&gt;
  Long: &lt;input id="length" name="length" type="checkbox" /&gt;&lt;br /&gt;
  
  &lt;input type="submit" value="Make Toast" /&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I really can't seem to understand why the same does work in Java (Eclipse) but not in C# (Xamarin). The only think that is really different is this:</p>
  
  <p>C#:</p>
  
  <pre><code>public class WebViewJavaScriptInterface : Java.Lang.Object
  </code></pre>
  
  <p>Java:</p>
  
  <pre><code>public class WebViewJavaScriptInterface
  </code></pre>
  
  <p>But I doubt that that has anything to do with it.</p>
  
  <p>I'd like to know how to solve this so that it does work in C#</p>