1

is it possible to make a html button that when I click, it will go to my android activity class. Just like using Intent when I view Activity to another Activity

anyone have a thoughts?

my JsInteface class change into this

 public class JavaScriptInterface {
    Context mContext;

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


    /** Show a toast from the web page */
    public void showToast(String toast) {
        Intent mainIntent = new Intent(mContext, echos.class); 
        mContext.startActivity(mainIntent); 

    }

}
user1635137
  • 25
  • 1
  • 6

1 Answers1

1
public class JavaScriptInterface {
Context mContext;

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

/** Show a toast from the web page */
public void showToast(String toast) {
    Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}

 WebView webView = (WebView) findViewById(R.id.webview);
 webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

in java script

   <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

  <script type="text/javascript">
    function showAndroidToast(toast) {
    Android.showToast(toast);
   }
  </script>
Athul Harikumar
  • 2,501
  • 18
  • 16
  • I already tried this one. But still don't have an Idea how to change the Toast method into Intent method – user1635137 Aug 30 '12 at 06:55
  • just change the code Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); to Intent mainIntent = new Intent(yourcontext, yheclass you want to go.class); youractivity.this.startActivity(mainIntent); – Athul Harikumar Aug 30 '12 at 07:00
  • should I change this one? public void showToast(String toast) – user1635137 Aug 30 '12 at 07:06
  • no need as its just the name of the function called from java script if you cahange that change the function name in java script too – Athul Harikumar Aug 30 '12 at 07:09
  • can you please take a look at my edited question above. because it still not working – user1635137 Aug 30 '12 at 07:15
  • change the JavaScriptInterface.this.startActivity(mainIntent); to mContext.startActivity(mainIntent); as JavaScriptInterface is not an activity – Athul Harikumar Aug 30 '12 at 07:18
  • I already change it into mContext but still not showing the echos.class – user1635137 Aug 30 '12 at 07:21
  • no there is no error. It just not showing the class that I declared in my Intent – user1635137 Aug 30 '12 at 07:24
  • try to run in the ui thread private final class JsInterface { public void launchIntent(final String payload) { Activity.this.runOnUiThread(new Runnable() { @Override public void run() { // use the payload if you want, attach as an extra, switch destination, etc. Activity.this.startActivity(new Intent(Activity.this, SomeOtherActivity.class)); } }); } } – Athul Harikumar Aug 30 '12 at 07:25
  • can you please edit it to codes format? its very hard to read – user1635137 Aug 30 '12 at 07:26
  • check http://stackoverflow.com/questions/4846296/android-sdk-webview-call-activity/4846692#4846692 change the function name and use it – Athul Harikumar Aug 30 '12 at 07:28