2

I would like to get user profile data; I'm following this tutorial :

http://code.tutsplus.com/tutorials/quick-tip-add-facebook-login-to-your-android-app--cms-23837

The above tutorial uses registerCallback method like this:

private LoginButton loginButton;
.
.
.
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {

    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException e) {

    }
});

now, How can I implement this method for a custom Button ?


below link use Facebook class, but for me this class undefined!

// Instance of Facebook Class
private Facebook facebook; 

http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/

S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

3 Answers3

6

You can use LoginManager to accomplish it without use the LoginButton.

First, you have to create the CallbackManager as usual:

callbackManager = CallbackManager.Factory.create();

Next, you have to register the callback created with LoginManager instance:

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {

    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException error) {
    }
});

And finally, you have to invoke the Facebook login with some permissions, for example:

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email"));

Don't forget override the onActivityResult method and call onActivityResult method from the CallbackManager:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
jos
  • 1,070
  • 12
  • 22
3

The first step was to include the built in facebook button in my xml file but set its visibility to "gone" so it cannot be seen by the user.

<com.facebook.widget.LoginButton
        android:id="@+id/authButton"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        />

In my main activity, I then defined the hidden facebook button and the button I wanted to use

fbLoginButton = (Button) findViewById(R.id.authButton); //built in facebook button
customButton = (Button) findViewById(R.id.customButton); //my custom button

Then in an onClickListener for the custom button, I used the ".performClick" method on the facebook button

customButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        fbLoginButton.performClick();
    }
});

This works like a charm for me. Hopefully this can help somebody.

@Rich Luick tnx.

Community
  • 1
  • 1
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
0

I give step by step code for make facebook button custom. check code here

you just have to make a frame layout.

Community
  • 1
  • 1
Harvi Sirja
  • 2,472
  • 2
  • 18
  • 19