Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have searched high and low and found so many examples of this, but unable to get it to work, my setup currently is:

Notifier.java

public class Notifier{  
    Context mContext;

    Notifier(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void showText()  
    {   
        Toast.makeText(mContext, "Some text!", Toast.LENGTH_LONG).show();
    }
}

SearchLicenseActivity.java

public class SearchLicenseActivity extends Activity {
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_license);

        LoadSearch();
    }

    public void LoadSearch(){
        webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);   
        webView.addJavascriptInterface(new Notifier(this), "Android");
        webView.loadUrl("javascript:Android.showText();");
    }
}

So I am expecting a Toast to show. It may be worth noting that this SearchActivity gets created when a button on a previous Activity is clicked; so I want it to execute LoadSearch straight away and get the Toast from the JavaScript.

I hope one of you out there can cure my woes over this!

Edit: I am also not getting any errors in LogCat.

share|improve this question
add comment

1 Answer

OK, it turns out webView must have a webpage loaded.

Even if the url is "about:blank" this seems to work. So...

webView.loadUrl("about:blank");
webView.loadUrl("javascript:Android.showText();");

...works.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.