Android Studio Facebook Login Share Like Tutorial – Part 2




After the Facebook App configuration from previous tutorial, you should have the Facebook App ID, if you haven’t got it or new to this, please have a look on the tutorial.

Tools Used
1. Android Studio 1.5.1 2.0
2. Facebook SDK 4.11.0

Todo
We will create an app that allows user to login using their facebook account and views some of the basic information. Other than that, we will add 2 more buttons, facebook share and like button to let user like and share. πŸ™‚

Want to make sure this code is working? No Problem, check out this video! πŸ™‚

View Demo – Android Studio Facebook Login Share Like Tutorial


Android Studio Facebook Login Share Like Tutorial – Part 2


Screen Layout
These are the 2 screens that we going to build. Basically, the 1st screen is for login and then the 2nd screen for the user to view basic information which we will get this information via Facebook GraphRequest. and also contains 1 facebook share button and like button.

1. Create Project

Let’s start this by creating a project and name it FB from Android Studio, see below for the rest of the setting. Please take note as the facebook requirement we need (API Level 15) or higher.

2. Add Facebook SDK to Your Project

To use Facebook SDK in a project, add it as a build dependency and import it, please navigate to app>build.gradle

3. Configure Facebook App ID

We obtain facebook app ID from the previous tutorial, so now is time to set this in our project. Please navigate to app>src>main>res>values>strings.xml and paste the following content

Please take note on the highlighted line, you need to replace with your Facebook App Id that getting from the previous tutorial.

3. Configure Facebook App ID in AndroidManifest.xml

The 2nd place that needs the Facebook App ID is manifest file, please go to app>src>main>AndroidManifest.xml and paste the following content

Please make sure you have all the highlighted lines in AndroidManifest.xml file and also take a look on the last highlighted section, you need to use your Facebook ID, for example ,com.facebook.app.FacebookContentProvider[APP_ID], change [APP_ID] with your ID, in our case the Facebook App Id is 1584671128490867.

4. Prepare The Resources

This is totally unnecessary step, but just for UI to look better by little (maybe not even 1%, πŸ˜‰ ), download these 4 images 1,2,3,4 or you can get it from the downloadable project, and paste in app>src>main>res>drawable

Icons made by Bogdan Rosu from www.flaticon.com is licensed by CC 3.0 BY
Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY

5. Code The Layout

There are two ways to implement Facebook login on Android

  • LoginButton – By using the method, we will need to place a button on the screen
  • LoginManager – Initiate nitiate login without using a UI element.
  • In this tutorial we will using the LoginButton method, so we’ll going to place the Facebook Login button in activity_login.xml but before that we like to customize a little on the button and so please navigate to app>src>main>res>values> replace with the following files content

    Now, please navigate to app>src>main>res>layout replace activity_login.xml with following content

    For the 2nd screen which will display logged in user’s basic information and have Facebook Share and Facebook Like button for you to play around πŸ™‚ , in the same folder, please add 1 more file and name it MainActivity.java and replace with following content

    6. Code

    Please navigate to app>src>main>java>com.instinctcoder.fb, and paste the following code

    And then in the same folder replace MainActivity.java with the following content

    That’s all, if you feel this tutorial not confuse you and want to get any future post, please follow us on facebook, google+, and tweeter. πŸ˜›




    JOIN OUR NEWSLETTER
    I agree to have my personal information transfered to MailChimp ( more information )
    In case you want to get any future post please allowed us deliver new posts direct into your inbox Or follow us on
    We hate spam. Your email address will not be sold or shared with anyone else.