Tuesday, 14 June 2016

Facebook Login using custom Button 


In this tutorial first of all if you are using eclipse then to download Facebook SDK from this website 
https://developers.facebook.com/docs/android/ and if you are using Android Studio then import dependencies in build.gradle file compile 'com.facebook.android:facebook-android-sdk:4.5.0'

To make the project in the https://developers.facebook.com/ and get the facebook id and add in to the string.xml file.

In XML file to add this code

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_facebook"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/fb_btn"
android:text="Facebook"
android:textAllCaps="false"
android:textColor="@android:color/white" />
</LinearLayout> 

In java file add this code

public class LoginActivity extends Activity implements View.OnClickListener {
Intent intent;
Button btn_facebook;
private CallbackManager mCallbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.login);
// Reference of Component
btn_facebook = (Button) findViewById(R.id.btn_facebook);
//Click Listener of Components
btn_facebook.setOnClickListener(this);
//Facebook login
List<String> permissions = new ArrayList<String>();
permissions.add("email");
FacebookSdk.sdkInitialize(this.getApplicationContext());
AppEventsLogger.activateApp(this);
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Success", "Login");
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response
Log.v("LoginActivity", response.toString());
try {
String id = object.getString("id");
String name = object.getString("name");
//String email = object.getString("email");
// Toast.makeText(getApplicationContext(), "email ="+email, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),Registration.class);
intent.putExtra("Fbname",name);
intent.putExtra("Fbid",id);
startActivity(intent);
overridePendingTransition(R.anim.open_next,R.anim.close_next);
finish();
} catch(Exception e){
e.printStackTrace();
}
}
});
//Toast.makeText(getApplicationContext(),""+response_code,Toast.LENGTH_LONG).show();
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this, "Login Cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(LoginActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_facebook:
loginFacebook();
break;
}
}
public void loginFacebook(){
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile","email","user_birthday","user_friends"));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
return;
}
}
}
In Android Manifest file you have to add the following meta tag
<activity    android:name="com.facebook.FacebookActivity"    android:label="@string/app_name"/>

<meta-data    android:name="com.facebook.sdk.ApplicationId"    android:value="@string/app_id" />
<meta-data    android:name="com.facebook.sdk.ApplicationName"    android:value="@string/app_name" />
<meta-data    android:name="com.facebook.sdk.WebDialogTheme"    android:resource="@android:style/Theme.Translucent.NoTitleBar" />

<provider    android:name="com.facebook.FacebookContentProvider"    android:authorities="com.facebook.app.FacebookContentProvider233936543368280"    android:exported="true" />