Add the Facebook activity to the AndroidManifest.xml file:
Add the login button to your layout XML file:
Now you have the Facebook button. If the user clicks on it, the Facebook login dialog will come up on top of the app's screen. Here the user can fill in their credentials and press the Log In button. If the credentials are correct, the dialog grants the corresponding permissions and a callback is sent to your original activity containing the button. The following code shows how you can receive that callback:
Create your own custom button for Facebook login
Once you first add the Facebook login/signup, the button looks something like:
Most of the times, it doesn't match with the design-specs of your app. And here's how you can customize it:
Just wrap the original com.facebook.login.widget.LoginButton into a FrameLayout and make its visibility gone.
Next, add your custom button in the same FrameLayout. I've added some sample specs. You can always make your own drawable background for the facebook button and set it as the background of the button.
The final thing we do is simply convert the click on my custom button to a click on the facecbook button:
Great! Now the button looks something like this:
How to add Facebook Login in Android
Add below dependencies to your build.gradle
Add below helper class to your utility package:
Add below code in Your Activity:
Add below code to your OnActivityResult:
Logging out of Facebook
Facebook SDK 4.0 onwards, this is how we logout:
For versions before 4.0, the logging out is gone by explicitly clearing the access token:
Setting permissions to access data from the Facebook profile
If you want to retrieve the details of a user's Facebook profile, you need to set permissions for the same:
You can keep adding more permissions like friends-list, posts, photos etc. Just pick the right permission and add it the above list.
Note: You don't need to set any explicit permissions for accessing the public profile (first name, last name, id, gender etc).
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0