Issue
Is it possible to create a share button (link) in my website that can invoke share dialogs in iOS and Android systems?
I mean the following dialog for each system:
I am not asking of how to do it by using iOS/Android SDKs. I want it with only HTML/JavaScript.
Solution
Yes, it is possible. Here is the link for it https://developer.android.com/guide/webapps/webview.html
First create html page and put below code in a button
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
Now, create a webpage in android.
WebView myWebView;
WebSettings webSettings;
WebAppInterface webInterface;
myWebView = (WebView) findViewById(R.id.webview);
webInterface=new WebAppInterface(MainActivity.this);
webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(webInterface, "Android");
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.loadUrl("Your url for that html file you created");
Now create a class which will listen to your callback above line addJavascriptInterface() is responsible for callback
public class WebAppInterface {
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();
}
}
Remember String is case sensitive,and android require @JavascriptInterface interface for calling android functionality
Answered By - Ranjan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.