Skip to content

Instantly share code, notes, and snippets.

@Asutosh11
Last active September 6, 2017 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Asutosh11/9dc51aeb88b50a2bd7b0ce55f00a79e1 to your computer and use it in GitHub Desktop.
Save Asutosh11/9dc51aeb88b50a2bd7b0ce55f00a79e1 to your computer and use it in GitHub Desktop.
Android Facebook login with custom Login button
build.gradle (Module: app)
==========================
// 1.
buildscript {
repositories {
mavenCentral()
}
}
// 2.
dependencies {
compile 'com.facebook.android:facebook-android-sdk:4.18.0'
}
AndroidManifest.xml
===================
Put this inside 'application' tag inside AndroidManifest XML file.
<!-- 1. -->
<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>
VERY VERY IMPORTANT TO NOTE
---------------------------
If you put the ids like this - android:value="525175644501670"
It crashes. Don't know why.
You should put it like this - android:value="@string/facebook_app_id"
activity_main.xml
=================
<!-- 1. -->
<TextView
android:id="@+id/fb_login"
android:background="#FFFFFF"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:text="Login with Facebook"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp" />
App.java
========
// This is the Application Context class.
// Put this inside onCreate of the Application Context class.
// Its important. Because Facebook SDK should be initialized before it is used
// 1.
FacebookSdk.sdkInitialize(getApplicationContext());
MainActivity.java
=================
// 1.
// Put this outside onCreate of Activity or outside onCreateView of Fragment.
TextView click;
CallbackManager callbackManager;
// 2.
// If you are using FB login in a Fragment, this should be inside the parent Activity of the Fragment
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
// 3.
callbackManager = CallbackManager.Factory.create();
click = (TextView)findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile"));
}
});
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment