title: Android : How to use javascript interface to show toast?
body: <p>I want to show toast from a webpage showed in a webview, like in the exemple from Google : <a href="http://developer.android.com/guide/webapps/webview.html" rel="nofollow">http://developer.android.com/guide/webapps/webview.html</a></p>
  
  <p>I have created a class for this named : <code>WebAppInterface.java</code> :</p>
  
  <p>package com.test.test;</p>
  
  <pre><code>import android.annotation.TargetApi;
  import android.app.ActionBar;
  import android.os.Bundle;
  import android.content.Context;
  import android.os.Build;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.NavUtils;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.TextView;
  import android.graphics.drawable.Drawable;
  import android.graphics.drawable.GradientDrawable;
  import android.graphics.Color;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.webkit.WebSettings;
  import android.webkit.WebChromeClient;
  import android.webkit.JavascriptInterface;
  import android.app.Activity;
  import android.view.KeyEvent;
  import android.app.ProgressDialog;
  import android.view.Window;
  import android.widget.Toast;
  import com.test.test.R;
  
  public class WebAppInterface extends Activity{
      Context mContext;
  
      /** Instantiate the interface and set the context */
      WebAppInterface(Context c) {
          mContext = c;
      }
  
      /** Show a toast from the web page */
      @JavascriptInterface
      public void showToast(String toast) {
          Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>
  
  <p>and here is my main activity code called <code>MainActivity.java</code> :</p>
  
  <pre><code>class JsObject {
      @JavascriptInterface
      public String toString() { return "injectedObject"; }
   }
  myWebView.addJavascriptInterface(new JsObject(), "injectedObject");
  </code></pre>
  
  <p>the page loaded by the web view contain the following code : </p>
  
  <pre><code>&lt;input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /&gt;
  
  &lt;script type="text/javascript"&gt;
      function showAndroidToast(toast) {
          Android.showToast(toast);
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>I follow exactly what is told in the Google exemple, but it dosent work... why ?</p>
  
  <p>Thanks !</p>