I am trying to login with facebook using android sdk 4.7. I have tried the following link http://www.theappguruz.com/blog/android-facebook-integration-tutorial http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/
This code works for me, try it out and check that you are using facebook sdk 4.7
package com.kushal.facebooklogin;
import java.util.Arrays;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.facebook.*;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class FacebookLogin extends FragmentActivity
{
private TextView tvfirst_name, tvlast_namee, tvfull_name, tvEmail;
private CallbackManager callbackManager;
LoginButton login_button;
String email,name,first_name,last_name;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.main);
tvfirst_name = (TextView) findViewById(R.id.first_name);
tvlast_namee = (TextView) findViewById(R.id.last_name);
tvfull_name = (TextView) findViewById(R.id.full_name);
tvEmail = (TextView) findViewById(R.id.email);
login_button = (LoginButton) findViewById(R.id.login_button);
login_button.setReadPermissions(Arrays.asList("public_profile","email"));
login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
@Override
public void onSuccess(LoginResult loginResult)
{
login_button.setVisibility(View.GONE);
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
@Override
public void onCompleted(JSONObject object, GraphResponse response)
{
Log.d("JSON", ""+response.getJSONObject().toString());
try
{
email = object.getString("email");
name = object.getString("name");
first_name = object.optString("first_name");
last_name = object.optString("last_name");
tvEmail.setText(email);
tvfirst_name.setText(first_name);
tvlast_namee.setText(last_name);
tvfull_name.setText(name);
LoginManager.getInstance().logOut();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,email");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
@Override
public void onCancel()
{
}
@Override
public void onError(FacebookException exception)
{
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
the xml design is as follow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF"
android:gravity="center"
android:orientation="vertical" >
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<TextView
android:id="@+id/last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<TextView
android:id="@+id/full_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
the mainefest file is as follow:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kushal.facebooklogin"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name=".FacebookLogin"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize" >
<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" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
</application>
</manifest>