Reputation: 411
In my app, I would like to show comments in my website by webview. This is what I have done:
In assets folder I have got a file named comment.html
<html>
<head>
</head>
<body>
<div id="fb-root"></div>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "http://connect.facebook.net/en_US/all.js#xfbml=1&appId=467390796664437";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-comments" data-href="http://www.haivl.com/photo/1259735" data-width="470"></div>
</body>
</html>
In CommentViewActivity.java
public class CommentViewActivity extends Activity {
public static Context context;
private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_zini_login);
context = this;
CookieSyncManager.createInstance(this);
CookieManager cm = CookieManager.getInstance();
cm.removeAllCookie();
webView = (WebView) findViewById(R.id.web_login);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/comment.html");
webView.setWebViewClient(new WebViewClientActivity());
}
public class WebViewClientActivity extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
System.out.println("onPageStarted: " + url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView webView, String url) {
System.out.println("onPageFinished: " + url);
}
}
}
The activity displays as follow:
As you can see, the facebook comment box obscured and I can't see whole comments. Anyone has a idea?
Thanks for your attention!
Upvotes: 0
Views: 3366
Reputation: 620
base on this post Failed to render Facebook comments on Android WebView via local HTML?
You need to specify a base URL.
myWebView.loadDataWithBaseURL("http://www.haivl.com",
"<html>
<head>
</head>
<body>
<div id="fb-root"></div>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "http://connect.facebook.net/en_US/all.js#xfbml=1&appId=467390796664437";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-comments" data-href="http://www.haivl.com/photo/1259735" data-width="470"></div>
</body>
</html>", "text/html", null, null);
Upvotes: 1