Reputation: 59
I want get and display the text from server in the WebView
environment and also with getting the message in the WebView
I want show the same message using the Toast
class. I wrote the following code But it does not display the text with Toast
class.
<?php
echo "Hello";
?>
And
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//WebView Object
WebView browser;
browser = (WebView) findViewById(R.id.webView);
//Enable Javascript
browser.getSettings().setJavaScriptEnabled(true);
//Inject WebAppInterface methods into Web page by having Interface name 'Android'
browser.addJavascriptInterface(new WebAppInterface(this), "Android");
//Load URL inside WebView
String s = "http://127.0.0.1:8080/apps/webview.php";
browser.loadUrl(s);
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/**
* Show Toast Message
* @param toast
*/
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}}
Upvotes: 1
Views: 906
Reputation: 2806
You have to call Android.showToast("hello")
Android is the name of your Javascript Interface
. Then by using that you can call the showToast()
Function
Call a JavaScript function
<script>
function showMessage(msg) {
Android.showToast(msg);
}
</script>
Upvotes: 1