Login to facebook using custom button

Asked

Viewed 864 times

6

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

I’m already getting it, though, I can only get it through 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 I created ?

2 answers

1

You call straight the following method:

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

Then, don’t forget to register the Callbacks and pass the call when 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>{
...
}

0

Hello Jonathan Sales this helps 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>
  • https://developers.facebook.com/docs/android/getting-started I don’t have much of a reputation for posting more than 2 links .-.

  • So, I also saw through Developers.facebook, but it only shows how to do from Loginbutton, which is from facebook itself, not by a convectional button that I create in XML.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.