Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  101] [ 5]  / answers: 1 / hits: 15351  / 9 Years ago, mon, august 24, 2015, 12:00:00

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:



enter



enter



I am not asking of how to do it by using iOS/Android SDKs. I want it with only HTML/JavaScript.


More From » android

 Answers
50

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


[#65314] Friday, August 21, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arturo

Total Points: 331
Total Questions: 99
Total Answers: 92

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
;