Boost Your Business with Expert Software Solutions: Book a Consultation

Steps to Integrate Facebook Login in Android

15+ Years of Experience

800+ Projects Completed

Flexible Engagement Models

100% client Satisfaction

Integrating Facebook is the easiest/simplest way in Android devices. Rather than custom login/registration we can do Facebook login in the android app using the current Facebook user account. We can authenticate people with their Facebook credentials. We can also enable the sharing of content through Facebook integration. We need to integrate the Facebook API in order to get all the user credentials from Facebook.

Before we deep dive into code, we should get facebook ID from facebook console. You can go through that URL and login with your current Facebook account. Create a project if not created by anyone. You need to redirect through “settings->Basic” on the side menu where you can find your Facebook ID. But before copying the ID we need to make sure that our project is added there. By scrolling down you can see the “Add Platform” label, clicking on it you’ll be given information on how to add the package name(Current project in android) and key hashes.

As you can see in the above picture I’ve created my project and add package name and key hashes. You can get the package name from the android studio in the manifest/Gradle file. Now the question is how do we get key hashes?

How to generate key hash?

We can either generate by using some of the commands present in the getting started Facebook for android SDK. The second way of generating key hash is by using code. You can use the below code on your First activity launch in onCreate method to print the generated key hash.

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Add code to print out the key hash try { PackageInfo info = getPackageManager().getPackageInfo( "com.facebook.samples.hellofacebook", 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) { } ... 

Note: This code will only generate debug SHA key, If you want to build a release SHA key then you need to follow the link which I’ve posted above. Using commands you can build release key hashes.

In your logcat in android studio, you can see the key hashes printed somewhat like this shown on the above picture.

Time to get into the code

In order to use the Facebook SDK for android application, you need to add some of the dependencies.

In your Gradle Scripts | build.Gradle (Project: module add the following dependency.

mavenCentral()

In your Gradle Scripts | build. Gradle (Module: app) module add the following dependency.

implementation 'com.facebook.android:facebook-android-sdk:[5,6)'

Save and Sync your project once in order to get all the functionalities used in facebook login SDK.

Now open your Strings xml file which is located under /app/res/values/strings.xml and add the following code

222005321890848
fb222005321890848

Replace your facebook ID which we generated recently in Facebook Console.

Make sure to add INTERNET permission inside the Manifest file.

uses-permission android:name="android.permission.INTERNET"

Stay remain inside the manifest file because now we need to add the facebook activity inside here.

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:label="@string/app_name" activity android:name="com.facebook.CustomTabActivity" android:exported="true" intent-filter action android:name="android.intent.action.VIEW" category android:name="android.intent.category.DEFAULT" category android:name="android.intent.category.BROWSABLE" data android:scheme="@string/fb_login_protocol_scheme" intent-filter activity 

It is used for opening up the facebook activity and also the use of intent filters for Chrome Custom Tabs.

Note - make sure you add these code lines inside the tag in Manifest file.

Designing facebook button

Go to the xml file where you want to use the facebook integration and paste the following code. Make sure you design the facebook button according to yourself.

We can also use the custom button if not the default one.

Register the callback class

callbackManager = CallbackManager.Factory.create();

Initialize the callback manager inside the onCreate method. This handles the login responses. If you are adding this in fragment then add it under onCreateView() method.

We need to set up some permissions in order to access the details from facebook.

Make sure if you are using the default facebook button then you are required to initialize with default button, other than you have to initialize facebook SDK with LoginManager if you are not using the default button.

Add the permissions whichever we needed them on call back listener. It sends you the result on what your requirements are.

Also we need to handle the onActivityResult method in order to read the result back from facebook SDK.

With default button

private static final String EMAIL = "email"; loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setReadPermissions(Arrays.asList(EMAIL)); // If you are using in a fragment, call loginButton.setFragment(this); // Callback registration loginButton.registerCallback(callbackManager, new FacebookCallback() { @Override public void onSuccess(LoginResult loginResult) { // App code } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); 

With custom button

callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback() { @Override public void onSuccess(LoginResult loginResult) { // App code } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); 

Make sure you add the call back class inside the onActivityResult

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { callbackManager.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data); } 

Tips to Hire a Mobile app development Company that can grab your vision.

Develop your first android mobile application using facebook login integration with Amar Infotech. In case any of your questions remain unanswered, you can directly talk to us, or leave us an email at sales@amarinfotech.com.

Voice Of Customers

Understand what drives customer satisfaction and loyalty through their genuine feedback and insights.

Happy Client Recommends Top Phoenix App DevelopersPhoenix Mobile App Development Company Client Testimonial

We've been working with Amar Infotech since 2014 on our project, Lyrics Guru. Their team provided top-notch Flutter Mobile App development along with a Laravel admin panel, perfectly meeting our needs.

Over the years, their consistent support and expertise have been invaluable to us.

We highly recommend Amar Infotech for their exceptional service and dedication to client success.

Happy Customer Recommends Top Sri Lanka Travel and Tour CMS Website Development CompanySatisfied Customer Reviews Best Travel and Tour CMS Websites in Sri Lanka

Hi, I'm Philipp Müller, CEO of a Tour and Travel Agency in Sri Lanka. We chose Amar Infotech to bring our website to life with their Tour Package Extranet Services.

hey delivered excellent service, developing a Laravel-based backend that allows us to create and customize our own tour packages easily.

I highly recommend Amar Infotech for their professionalism and expertise in web development.

Client Testimonial - Best Travel App in the USA - Goeasyaviation and GoeasyticketPositive Feedback on Goeasyaviation and Goeasyticket Air Shipping, Flight, and Hotel Booking

Hello, my name is Tito. We would like to extend our gratitude to Amar Infotech for their invaluable assistance in developing our website and app.

They were extremely helpful throughout the entire journey, guiding us every step of the way. Their professionalism is second to none.

Thanks to them, we now have a fully functional Flight Booking website that perfectly meets our needs.

Professional Travel Website Development: Amar Infotech - Sammy Tours Positive ReviewTop-Rated Travel Website Agency: Sammy Tours Client Testimonial for Amar Infotech

I am the owner of Sammy Tours Sri Lanka, and I am thrilled with Amar Infotech for designing and developing my travel and tourism website.

They created a custom website that perfectly showcases our tour packages for Sri Lanka. Their expertise and dedication have significantly impacted our business.

I highly recommend Amar Infotech for their exceptional service and commitment to excellence.

Why Amar Infotech ?

On-demand, Flexible

Select the ideal remote team tailored to your business needs.

Cost Benefit

Enjoy up to 30% savings with our competitive solutions.

Experts Talent

Leverage the expertise of over 100 skilled IT professionals.

Risk Free Delivery

Ensure success with agile project management and consistent, reliable outcomes.

Innovative Solutions

Benefit from cutting-edge technology and creative approaches that drive your business forward.

Download Our Brochure for Exclusive Insights!

Discover how we drive success with innovative solutions tailored for your needs. Get a comprehensive overview of our expertise and achievements in just one click.

Download Now

Awards & Recognitions

Celebrating our journey of excellence with accolades that inspire us to reach new heights.

Amar Infotech Top Software Developers - Clutch
Top Software Development Company Amar Infotech - Goodfirms
Amar Infotech Best Company to Work with - GoofFirms
Top 10 Custom Software Development Company - Amar Infotech
Amar Infotech Top Rated Software Development Company
Top Web Development Company - Amar Infotech

Global Presence & Representative

Expanding horizons with a global network of representatives dedicated to delivering excellence across borders.

India (HQ)

4th Floor, Sunrise Avenue, Stadium - Commerce Six Road, Ahmedabad, INDIA sales@amarinfotech.com

USA

AI Jones (Account Executive), Phoenix, AZ 85013, 1.623.205.2415 ajones@amarinfotech.com

Canada

Ronak Patel (Sales Person), 64 caranci crescent Brampton ON Canada ronak@amarinfotech.com

Australia

Jessica (Sales Person), 9 nirimba drive quackers hill nsw 2763 jessica@amarinfotech.com

UK

George (Sales Person) in London, UK george@amarinfotech.com

Netherlands

Adam (Sales Person) in Breda, Netherlands adam@amarinfotech.com