Login to facebook using custom button

6

In a project I'm doing, I have to log in through Facebook.

I'm already getting that, however, I only get this for LoginButton of the Facebook SDK itself using these methods:

private static final List<String> PERMISSIONS = Arrays.asList("email", "public_profile", "user_friends");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_login);

    btnFacebook();

    LoginButton fbLogin = (LoginButton) findViewById(R.id.fbLogin);

    fbLogin.setPublishPermissions(Arrays.asList("email", "public_profile", "user_friends"));

    Session.openActiveSession(LoginActivity.this, true, new Session.StatusCallback() {
        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                         Exception exception) {
            if (session.isOpened()) {

                List<String> permissions = session.getPermissions();
                if (!isSubsetOf(PERMISSIONS, permissions)) {
                    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                            LoginActivity.this, PERMISSIONS);
                    session.requestNewReadPermissions(newPermissionsRequest);
                    return;
                }
                // make request to the /me API
                Request.newMeRequest(session, new Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        //TODO Auto-generated method stub
                        try {

                            Log.i("Name", "" + user.getFirstName() + " " + user.getLastName());
                            Log.i("Email", "" + user.getProperty("email").toString());
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                }).executeAsync();

            }
        }
    });
}

private boolean isSubsetOf(Collection<String> subset,
                           Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}

How do I use a button, what do I do?

    
asked by anonymous 15.04.2015 / 18:26

2 answers

1

You call the following method directly:

LoginManager.getInstance().logInWithReadPermissions(activity, Arrays.asList("public_profile"));

Then, do not forget to register the Callbacks and pass the call when the onActivityResult is called

mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookLoginCallback());
...
 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
...
private class FacebookLoginCallback implements FacebookCallback<LoginResult>{
...
}
    
13.05.2016 / 03:25
0

Hello Jonathan Sales does this help you?

xmlns:*PREFIX*="http://schemas.android.com/apk/res-auto"
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fb="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.facebook.widget.LoginButton
android:id="@+id/connectWithFbButton"
style="@style/com_facebook_loginview_default_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:text="@string/connect_with_facebook"
fb:login_text="@string/connect_with_facebook"
fb:logout_text="Connecting with facebook" />

</LinearLayout>
    
15.04.2015 / 18:30