Facebook integration (Custom Button)
Step 1 : Do all actions mentioned in link for fb login https://developers.facebook.com/docs/facebook-login/android. (Ex: sha1 key,release key,etc..,)
Step 2 : create MainActivity like below attached find it.
Step 3 : create layout file like below attached find it.
Step 5 : Run execute and watch the output in layout. Over.
Step 6 : Comment this tutorial below or write what you want.
Step 2 :public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tvFacebookLogin;
loginButton.setReadPermissions(Arrays.asList("public_profile"/*, "user_friends", "email", "user_birthday"*/));
// LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.e("onSuccess", "--------" + loginResult.getAccessToken());
Log.e("Token", "--------" + loginResult.getAccessToken().getToken());
Log.e("Permision", "--------" + loginResult.getRecentlyGrantedPermissions());
// Profile profile1 = Profile.getCurrentProfile(); // not working herer
// callToast(profile1.getFirstName()); // not working herer ,work at graph request
// Log.e("Image URI", "--" + profile.getLinkUri());
Log.e("OnGraph", "------------------------");
callToast("facebook login success");
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.d("Lokesh", "response is :"+response.toString() + " JosnObject is :"+object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,last_name,email");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
callToast("facebook login cancel");
}
@Override
public void onError(FacebookException exception) {
callToast("facebook login error");
Log.d("Lokesh","exception is :"+exception.toString());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
accessTokenTracker.stopTracking();
}
private void initializeViews() {
loginButton = (LoginButton) findViewById(R.id.login_button);
tvFacebookLogin = (TextView) findViewById(R.id.activitylogin_loginbyfb_tv);
tvFacebookLogin.setOnClickListener(this);
}
});
loginButton.setReadPermissions("email");
}
@Override
public void onClick(View view) {
loginButton.performClick();
}
}
Step 3 :
<FrameLayout
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/activitylogin_loginbygmail_tv">
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:visibility="gone"/>
<TextView
android:id="@+id/activitylogin_loginbyfb_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/bp_margin_20"
android:layout_marginStart="@dimen/bp_margin_20"
android:layout_marginRight="@dimen/bp_margin_20"
android:layout_marginEnd="@dimen/bp_margin_20"
android:drawableLeft="@drawable/ic_fb"
android:background="@drawable/roundcorner_blue"
android:layout_marginTop="@dimen/bp_margin_10"
android:padding="@dimen/bp_margin_5"
android:gravity="center_vertical"
android:textSize="@dimen/bp_margin_18"
android:textColor="@color/white"
android:drawablePadding="@dimen/margin_25"
android:text="@string/login_through_facebook"/>
Step 5:
09-26 03:41:04.308 13113-13113/mycompany.com.doctorapp D/Lokesh: response is :{Response: responseCode: 200, graphObject: {"id":"764057633737017","first_name":"Lokesh","last_name":"Kondaparthi","email":"lokeshit13@gmail.com"}, error: null} JosnObject is :{"id":"764057633737017","first_name":"Lokesh","last_name":"Kondaparthi","email":"lokeshit13@gmail.com"}