Facebook Login In Android


Goto https://developers.facebook.com and sign in using your credentials, then click Add New App Button.
Facebook Login In Android
Put the application name and contact email ID on the page and click Create App ID button.
There you will see a page like the following, from there please copy the value of App ID,

Facebook Login In Android
In the bottom, there will be one button named Add Platform and click on this button and select Android.
There you need to enter the package name and hash code of your Android Studio.
The package name can be copied from the manifest file.
Facebook Login In Android
For generating the hash code run the following code
  1. try {  
  2.     PackageInfo info = getPackageManager().getPackageInfo("demoapp.techniche.com.fblogin", PackageManager.GET_SIGNATURES);  
  3.     for (Signature signature: info.signatures) {  
  4.         MessageDigest md = MessageDigest.getInstance("SHA");  
  5.         md.update(signature.toByteArray());  
  6.         Log.e("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));  
  7.     }  
  8. catch (PackageManager.NameNotFoundException e) {} catch (NoSuchAlgorithmException e) {}  
After entering all the fields please click on save changes. That time if the package name is not listed in the Google Play store then it will show one dialogue, and there you have to select "Use this package name".
Now, you can go to the project and in the gradle.build a file of your project adds the following line.
  1. repositories {  
  2.     mavenCentral()  
  3. }  
And in the applications gradle.build file adds the following dependency.
  1. compile 'com.facebook.android:facebook-android-sdk:4.+'  
And click sync now. This will add the Facebook SDK into your application.
Now open your string.xml file and add the APP ID you selected form developer.facebbok.com into here.
  1. <string name="face_book_app_id">1234564545454</string>  

Now, go to the manifest file and put the following code inside of the application tag.

  1. <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/face_book_app_id" />  
Initialize Facebook SDK in your onCreate of Activity/Fragment.
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     FacebookSdk.sdkInitialize(this.getApplicationContext());.....  
  5. }  
Create callback manager and graphrequest objects.
  1. CallbackManager callbackManager;  
  2. GraphRequest request;  
Intialize the callbackManager in oncreate()
  1. callbackManager = CallbackManager.Factory.create();  
And register the lister in the onCreate() like this.
  1. LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback < LoginResult > () {  
  2.             @Override  
  3.             public void onSuccess(final LoginResult loginResult) {  
  4.                 Log.e("ID""success");  
  5.                 request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {  
  6.                         @Override  
  7.                         public void onCompleted(JSONObject object, GraphResponse response) {  
  8.                             Log.e("LoginActivity", response.toString());  
  9.                             if (object != null) {  
  10.                                 String providerId = object.optString("id");  
  11.                                 String email = object.optString("email");  
  12.                             }  
  13.                         }); Bundle parameters = new Bundle(); parameters.putString("fields""id,name,email,gender, birthday"); request.setParameters(parameters); request.executeAsync();  
  14.                 }  
  15.                 @Override  
  16.                 public void onCancel() {  
  17.                     Log.e("Facebook Cancel""Cancel");  
  18.                 }  
  19.                 @Override  
  20.                 public void onError(FacebookException error) {  
  21.                     Log.e("Facebook Error""Error");  
  22.                 }  
  23.             });  
Finally, in create onActivityResult for the Facebook signin.
  1. @Override  
  2. public void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.     super.onActivityResult(requestCode, resultCode, data);  
  4.     callbackManager.onActivityResult(requestCode, resultCode, data);  
  5. }  
Create a sample button in your activity layout and on clicking of that button call the following lines of code to login with Facebook.
  1. facebook.setOnClickListener(new View.OnClickListener() {  
  2.     @Override  
  3.     public void onClick(View view) {  
  4.         LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("user_photos""email""user_birthday""public_profile"));  
  5.     }  
  6. });  
Happy coding.