Skip to content

Instantly share code, notes, and snippets.

@jevhee
Created September 22, 2017 05:48
Show Gist options
  • Save jevhee/1e9c9abb177134d648b1afe440da6d54 to your computer and use it in GitHub Desktop.
Save jevhee/1e9c9abb177134d648b1afe440da6d54 to your computer and use it in GitHub Desktop.
Create Facebook widget Comment android
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cl_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:subtitleTextAppearance="@style/SubtitleStyle"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:titleTextAppearance="@style/TitleLightStyle" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<im.delight.android.webview.AdvancedWebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
package com.inagata.news.views.activity.comment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import com.inagata.news.BuildConfig;
import com.inagata.news.R;
import com.inagata.news.utils.IConstans;
import com.inagata.news.views.base.BaseActivity;
import butterknife.BindView;
import im.delight.android.webview.AdvancedWebView;
/**
* Created by M on 07/09/2017.
*/
public class CommentActivity extends BaseActivity implements AdvancedWebView.Listener {
private static final String TAG = "CommentActivity";
private static final int NUMB_POST = 10;
private static final String APP_ID = "YOUR APP_ID";
private String mUrlComment;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.swipe_refresh)
SwipeRefreshLayout swipe_refresh;
@BindView(R.id.web_view)
AdvancedWebView webViewComments;
public static void startThisActivity(Context context, String Url) {
Intent intent = new Intent(context, CommentActivity.class);
intent.putExtra(IConstans.Extra.EXTRA_URL, Url);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
@Override
protected int getLayoutView() {
return R.layout.activity_comment;
}
@Override
protected void initComponent(@Nullable Bundle savedInstanceState) {
initToolbar();
initData();
initRefresh();
}
@Override
protected void onResume() {
webViewComments.onResume();
super.onResume();
}
@Override
protected void onPause() {
webViewComments.onPause();
super.onPause();
}
@Override
public void onBackPressed() {
if (!webViewComments.onBackPressed()) {
return;
}
super.onBackPressed();
}
private void initToolbar() {
setSupportActionBar(toolbar);
ActionBar mActionBar = getSupportActionBar();
assert mActionBar != null;
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowTitleEnabled(true);
mActionBar.setHomeAsUpIndicator(R.drawable.ic_back);
mActionBar.setTitle(getString(R.string.label_comment));
}
private void initData() {
mUrlComment = getIntent().getStringExtra(IConstans.Extra.EXTRA_URL);
if (mUrlComment != null) {
loadComments();
}
}
private void initRefresh() {
swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipe_refresh.setRefreshing(true);
webViewComments.reload();
}
});
}
private void loadComments() {
webViewComments.setListener(this, this);
webViewComments.setDesktopMode(false);
webViewComments.setCookiesEnabled(true);
webViewComments.setThirdPartyCookiesEnabled(true);
String html = createFacebookCommentWidget();
webViewComments.loadDataWithBaseURL(BuildConfig.BASE_URL, html, "text/html", "UTF-8", null);
}
private String createFacebookCommentWidget() {
return "<!DOCTYPE html>\n" +
"<html lang=\"id\">\n" +
"<body>\n" +
"<div id=\"fb-root\"></div>\n" +
"<script>\n" +
"(function(d, s, id) {\n" +
"var js, fjs = d.getElementsByTagName(s)[0];\n" +
"if (d.getElementById(id)) return;\n" +
"js = d.createElement(s); js.id = id;\n" +
"js.src = \"https://connect.facebook.net/id_ID/sdk.js#xfbml=1&version=v2.10&appId=\"" + APP_ID + "\";\n" +
"fjs.parentNode.insertBefore(js, fjs);\n" +
"}(document, 'script', 'facebook-jssdk'));\n" +
"</script>\n" +
"<div class=\"fb-comments\" data-href=\"" + mUrlComment + "\" \n" +
"data-numposts=\"" + NUMB_POST + "\" data-order-by=\"reverse_time\"></div>\n" +
"</body>\n" +
"</html>";
}
@Override
public void onPageStarted(String url, Bitmap favicon) {
Log.d(TAG, "onPageStarted: " + url);
swipe_refresh.setRefreshing(true);
}
@Override
public void onPageFinished(String url) {
Log.d(TAG, "onPageFinished: " + url);
boolean isRemovePopup = url.contains("/plugins/close_popup.php?reload");
Log.d(TAG, "onPageFinished: " + isRemovePopup);
if (isRemovePopup) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
loadComments();
}
}, 600);
}
if (swipe_refresh.isRefreshing()) {
swipe_refresh.setRefreshing(false);
}
}
@Override
public void onPageError(int errorCode, String description, String failingUrl) {
Log.d(TAG, "onPageError: " + failingUrl);
}
@Override
public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
}
@Override
public void onExternalPageRequest(String url) {
}
}
// Add dependencies AdvancedWebView(Enhanced WebView component for Android that works as intended out of the box)
compile 'com.github.delight-im:Android-AdvancedWebView:v3.0.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment