This tutorial is all about integration of an Android application to the Facebook using Android Studio and how we can login into an application using Facebook credentials
1. Facebook Integration with Android Application
1.1 Open Facebook Developer Panel
Visit developers.facebook.com
1.2 Open MyApps Tab On Developer Panel
Choose the MyApps tab and click on the “Add a New App” and choose android.
1.3 Create New Facebook App
Create a new facebook App.
1.4 Create New Facebook App ID
Create a new facebook App ID by clicking on the button “Create New Facebook App ID”.
1.5 Add Facebook SDK To Project
In your android Studio, goto File ⇒ New ⇒ New Projects. Set your project name and the Package Name and click Next.
Select API 15: Android 4.0.3 or higher and create your new project.
Set your package name and the Main Activity name in the developer panel of Facebook and click on Next.
Generate an android key hash, which will ensure the authenticity of the integration of your App and Facebook.
Open your Terminal (CMD) and run the given below command on Mac and any Linux OS to generate an android key hash.
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
On Windows:
keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%.androiddebug.keystore | openssl sha1 -binary | openssl base64
While generating an android key hash you have to provide a random password like test123.If password is not asked then keystore path is incorrect.
Copy and Paste the android key hash to the Facebook developer panel.
Open your android project
Add the given below line to Module-level /app/build.gradle before dependencies.
repositories { mavenCentral() }
Add the compile dependency with the latest version of the Facebook SDK in the /app/build.gradle file.
compile ‘com.facebook.android:facebook-android-sdk:4.6.0’
And then, build your project and now you can import the facebook SDK (com.facebook.FacebookSdk) to your project.
1.6 Add Facebook App Id
Add Facebook App Id in your project’s strings file and update your Android manifest file.
16691858xxxxxxxx
1.7 Open your project’s AndroidManifest.xml file add the following
Add internet permission by adding uses-permission element to the manifest file.
1.8 Add Meta-Data Tag In Manifest File
Add a meta-data element into the application element in manifest file.
1.9 Add FacebookActivity In Manifest File
Add the FacebookActivity to the manifest file to use Facebook login.
2. Facebook Login
2.1 Open your activity_main.xml file and add the facebook Login Button
2.2 Initialize Facebook SDK
Now we need to initialize the facebook SDK by calling FacebookSdk.sdkInitialize and then callback manager will handle the login responses by calling CallbackManager.Factory.create. Add the following line of code to the MainActivity.java file and call it before writing any line of code into the onCreate method.
/*Initialize the facebook sdk
And then callback manager will handle the login responses*/
protected void facebookSDKInitialize() { FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); facebookSDKInitialize(); setContentView(R.layout.activity_main); LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setReadPermissions(“email”); getLoginDetails(loginButton); }
2.3 Register Callback With LoginButton
Now you need to add a method which will register a callback with LoginButton to respond to the login result.
Add the following line of code to the MainActivity.java file and call it into the onCreate method.
//Register a callback function with LoginButton to respond to the login //result.
protected void getLoginDetails(LoginButton login_button){ // Callback registration login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult login_result) { Intent intent = new Intent(MainActivity.this,HomePage.class); startActivity(intent); } @Override public void onCancel() { // code for cancellation } @Override public void onError(FacebookException exception) { // code to handle error } }) }
2.4 Forword Login Result To CallbackManager
Finally you should call callbackManager.onActivityResult, which will forward the login results to the callbackManager created in onCreate method :
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); }
2.5 Create HomeActivity
We create a Activity java file called HomeActivity. In this Activity, we make a button to logout from facebook.
public class HomePage extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); (findViewById(R.id.logout)).setOnClickListener(new View.OnClickListener() { @Override< public void onClick(View v) { LoginManager.getInstance().logOut(); Intent intent = new Intent(HomeActivity.this,MainActivity.class); startActivity(intent); } }); } }
Contributor: Ashiqur Rahman, Nascenia