Android facebook login email

To implement Facebook login in an Android app using the Facebook SDK, you'll need to follow these steps:

Step 1: Create a Facebook Developer Account and App

  1. Go to the Facebook for Developers website and create a new account if you don't already have one.
  2. Create a new app by clicking on "Add New App" and filling out the required information.
  3. Note down the App ID and App Secret, you'll need them later.

Step 2: Add Facebook SDK to Your Android Project

  1. Add the Facebook SDK to your Android project by adding the following dependency to your build.gradle file:
    dependencies {
     implementation 'com.facebook.android:facebook-android-sdk:8.2.0'
    }
  2. Sync your project with Gradle files.

Step 3: Configure Facebook SDK

  1. Create a new file called FacebookConfig.java and add the following code:

    public class FacebookConfig {
     public static final String APP_ID = "YOUR_APP_ID";
     public static final String APP_SECRET = "YOUR_APP_SECRET";
    }

    Replace YOUR_APP_ID and YOUR_APP_SECRET with the values you noted down in Step 1.

  2. Create a new file called FacebookLoginActivity.java and add the following code:

    public class FacebookLoginActivity extends AppCompatActivity {
     private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
         @Override
         public void onSuccess(LoginResult loginResult) {
             // Login successful, get the user's email
             GraphRequest request = GraphRequest.newMeRequest(
                     loginResult.getAccessToken(),
                     new GraphRequest.GraphJSONObjectCallback() {
                         @Override
                         public void onCompleted(JSONObject object, GraphResponse response) {
                             try {
                                 String email = object.getString("email");
                                 // Use the email to login or register the user
                             } catch (JSONException e) {
                                 e.printStackTrace();
                             }
                         }
                     });
             request.executeAsync();
         }
    
         @Override
         public void onCancel() {
             // Login cancelled
         }
    
         @Override
         public void onError(FacebookException e) {
             // Login error
         }
     };
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         FacebookSdk.sdkInitialize(this);
         callbackManager = CallbackManager.Factory.create();
         loginButton = (LoginButton) findViewById(R.id.login_button);
         loginButton.setReadPermissions("email");
         loginButton.registerCallback(callbackManager, callback);
     }
    
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
         callbackManager.onActivityResult(requestCode, resultCode, data);
     }
    }

    Step 4: Add Facebook Login Button to Your Layout

  3. Add a LoginButton to your layout file (activity_main.xml for example):

    <com.facebook.login.widget.LoginButton
     android:id="@+id/login_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="16dp" />

    Step 5: Start the Facebook Login Activity

  4. Start the FacebookLoginActivity when the user clicks the login button:

    loginButton.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent intent = new Intent(FacebookLoginActivity.this, FacebookLoginActivity.class);
         startActivity(intent);
     }
    });

    That's it! With these steps, you should now have a basic Facebook login implementation in your Android app.