Ares Ares - 1 month ago 22
Android Question

Embed Twitter Widget in Android webview

I am trying to embed a twitter widget into a webview, but it feels like the JS code is not executing. All that is displayed is "Twitter by @xxxxxx"

WebView webView = new WebView(getActivity());

webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_LONG).show();
}
});

String summary = "<a class=\"twitter-timeline\" href=\"https://twitter.com/"+getArguments().getString("handle")+"\" data-widget-id=\""+getArguments().getString("widgetId")+"\">Tweets by @"+getArguments().getString("handle")+"</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\"://platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>";
webView.loadData(summary, "text/html", null);

return webView;


My application does have Intenet access permission

Answer

try webView.loadDataWithBaseUrl and set the base url to https://twitter.com