Facebook Login Using LoginButton
Facebook SDK give two classes in Android to Login through Facebook
1. LoginButton class which provides UI of Login button and maintain the Login and Logout session.
2. LoginManager class which provides initiate login without using UI element.
In This tutorial we will use LoginButton class.
Below Step by Step process to Facebook Login using LoginButton.
1. Open Facebook Developer Account.
2. Go to MyApps and click on Add new app then the below pop-up window opened.
3. Select Android from above popup. The below window opened.
4. Type your app name (App name must not be contain word 'face' in it) and press skip quick start button. The below window opened.
5. Open Android Studio and create a project.
6. Now open app level gradle and add the below dependency.
7. Open xml file and Add LoginButton into xml like below.
8. Now we have to initialize the Facebook SDK with calling
9. Open Java Class file and initialize Facebook before setContentView() like below code.
10. Create CallBackManager class instance for handling login responses.
11. Now create LoginButton view instance with button id and register Callback to LoginButton. and override FacebookCallBack methods.
12. Override Activity's OnActivityResult() and forward the login result to callbackManager.
13. Now Its time time get KeyHash for your application. Write the below code in onCreate() and copy the hashKey from Log and Remove this code after getting HashKey.
14. Open Facebook Developer's site and open App. Go to setting select Add plateform and Add Android.
15. Add project package name, class name and HashKey to fields and save changes.
16. Copy Facebook app Id and save it to Project's string.xml file with facebook_app name;
17. Open AndroidManifest.xml and write below code and register facebook activity and provider in manifest.
18. write Internet permission in android manifest file.
Its all done for this tutorial.
Happy Coding.
1. LoginButton class which provides UI of Login button and maintain the Login and Logout session.
2. LoginManager class which provides initiate login without using UI element.
In This tutorial we will use LoginButton class.
Below Step by Step process to Facebook Login using LoginButton.
1. Open Facebook Developer Account.
2. Go to MyApps and click on Add new app then the below pop-up window opened.
5. Open Android Studio and create a project.
6. Now open app level gradle and add the below dependency.
compile 'com.facebook.android:facebook-android-sdk:4.4.0'
7. Open xml file and Add LoginButton into xml like below.
<com.facebook.login.widget.LoginButton android:id="@+id/login_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" />
8. Now we have to initialize the Facebook SDK with calling
FacebookSdk.initialize(Context).
9. Open Java Class file and initialize Facebook before setContentView() like below code.
super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); setContentView(R.layout.activity_main);
10. Create CallBackManager class instance for handling login responses.
CallbackManager callbackManager = CallbackManager.Factory.create();
11. Now create LoginButton view instance with button id and register Callback to LoginButton. and override FacebookCallBack methods.
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
12. Override Activity's OnActivityResult() and forward the login result to callbackManager.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); }
13. Now Its time time get KeyHash for your application. Write the below code in onCreate() and copy the hashKey from Log and Remove this code after getting HashKey.
try { PackageInfo info = getPackageManager().getPackageInfo( getPackageName(), PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d("KeyHash : ", Base64.encodeToString(md.digest(), Base64.DEFAULT)); } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { }
14. Open Facebook Developer's site and open App. Go to setting select Add plateform and Add Android.
15. Add project package name, class name and HashKey to fields and save changes.
16. Copy Facebook app Id and save it to Project's string.xml file with facebook_app name;
17. Open AndroidManifest.xml and write below code and register facebook activity and provider in manifest.
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<activity android:name="com.facebook.FacebookActivity" android:configChanges= "keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:label="@string/app_name" /> <provider android:authorities="com.facebook.app.FacebookContentProvider1234" android:name="com.facebook.FacebookContentProvider" android:exported="true" />
18. write Internet permission in android manifest file.
<uses-permission android:name="android.permission.INTERNET"/>
Java class File code look like: -
public class MainActivity extends AppCompatActivity { LoginButton loginButton; CallbackManager callbackManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); setContentView(R.layout.activity_main); callbackManager = CallbackManager.Factory.create(); loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_SHORT).show(); } @Override public void onCancel() { Toast.makeText(getApplicationContext(), "Login cancel", Toast.LENGTH_SHORT).show(); } @Override public void onError(FacebookException exception) { Toast.makeText(getApplicationContext(), "Exceptions", Toast.LENGTH_SHORT).show(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } }
AndroidManifest File looks like: -
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.amanarora.facebooklogin.facebooklogin"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id" /> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.facebook.FacebookActivity" android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <provider android:name="com.facebook.FacebookContentProvider" android:authorities="com.facebook.app.FacebookContentProvider1234" android:exported="true" /> </application> </manifest>
Its all done for this tutorial.
Happy Coding.
Comments
Post a Comment