How do i integrate Facebook Login in Android app?

The Activity Layout File

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:facebook=”http://schemas.android.com/apk/res-auto&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center_horizontal”
android:orientation=”vertical”
android:padding=”20dp” >

<com.facebook.login.widget.LoginButton
android:id=”@+id/login_button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center_horizontal”
android:layout_marginTop=”30dp”
android:layout_marginBottom=”30dp” />

</LinearLayout>

===

The Activity

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.yourapp.R;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;

public class FBConnect1 extends Activity {

private com.facebook.login.widget.LoginButton loginButton;
private Button logoutBtn;
private Button closeBtn;
private TextView userName;

boolean isFetching = false;
CallbackManager callbackManager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println(“FACEBOOK STARTING”);
FacebookSdk.sdkInitialize(getApplicationContext());

setContentView(R.layout.activity_fbconnect1);

callbackManager = CallbackManager.Factory.create();

loginButton = (com.facebook.login.widget.LoginButton) findViewById(R.id.login_button);

loginButton.setReadPermissions(“user_friends”);
// If using in a fragment
// loginButton.setFragment(this);
// Other app specific specialization

// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback() {
@Override
public void onSuccess(LoginResult loginResult) {
System.out.println(“SUCCESS FACEBOOK LOGIN”);

Profile profile = Profile.getCurrentProfile();
System.out.println(“FACEBOOK PROFILE onSuccess ” + profile);
System.out.println(“FACEBOOK USER: ” + profile.getName());

// Get User Name
//mTextDetails.setText(profile.getName() + “”);
//loginResult.
// App code
}

@Override
public void onCancel() {

System.out.println(“FACEBOOK SUCCESS FACEBOOK CANCELED”);
// App code
}

@Override
public void onError(FacebookException exception) {

System.out.println(“FACEBOOK ERROR : ” + exception);
// App code
}
});
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: