you do not need to import facebook SDK files in android studio just go to your app module gradle: build.gradle and paste
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:21.0.3'
compile 'com.facebook.android:facebook-android-sdk:4.8.0'
}
.Do not forget to sync
. at this time min sdk version must me 15
and go to https://developers.facebook.com/ then create your app id and paste it in values >> trings.xml also do not forget to create your app keyHASH .
in your project create the following file:MyApplication.java
paste in these codes:
public class MyApplication extends Application {
// Updated your class body:
@Override
public void onCreate() {
super.onCreate();
// Initialize the SDK before executing any other operations,
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}
Make your make your SigninFragment like this :
public class SigninFragment extends Fragment {
CallbackManager callbackManager;
private LoginButton loginButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Other app specific specialization
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(getActivity());
callbackManager = CallbackManager.Factory.create();
// Initialize the SDK before executing any other operations,
View rootView = inflater.inflate(R.layout.signin_fragment, container, false);
loginButton = (LoginButton)rootView.findViewById(R.id.login_button);
loginButton.setReadPermissions("email");
// If using in a fragment
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(), "Login attempt succes.",Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Login attempt cancelled.",Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getApplicationContext(), "Login attempt failed.",Toast.LENGTH_LONG).show();
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
How your android manifest must look like:
//facebook
<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"
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>
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
//end facebook
What must be found in strings.xml :
<string name="facebook_app_id">here there is id number you get from facebook developpers</string>
<string name="fb_login_protocol_scheme">fbAPP_ID</string>
I your signinFragment.xml add facebook button like this :
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="71dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />