How to login facebook using sdk 4.1.0 in android? -
[i"m writing codes login page application.i have made login successful given email , password i"m facing problem while writing codes login facebook.i have checked https://developers.facebook.com/docs/facebook-login/android/v2.3
but still i"m getting errors while running code per instructions.i"m new android developing...so proper praiseworthy.thankx in advance.]1
here i"m providing working solution facebook login sdk4.
first of add dependency on build.gradle(module app) under dependencies {......
compile 'com.facebook.android:facebook-android-sdk:4.1.0'
} sync gradle.... thereafter,in android manifest following changes:-
<uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_network_state"/> <uses-permission android:name="android.permission.get_accounts" /> <uses-permission android:name="android.permission.use_credentials" /> <meta-data android:name="com.facebook.sdk.applicationid" android:value="@string/app_id"/> //write app_id u have generated in res/values/strings.xml folder. <provider android:authorities="com.facebook.app.facebookcontentprovider456579934439214" android:name="com.facebook.facebookcontentprovider" android:exported="true" /> <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" />
after write following codes in ur mainactivity class:
public class mainactivity extends actionbaractivity { loginbutton loginbutton; callbackmanager callbackmanager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); facebooksdk.sdkinitialize(this.getapplicationcontext()); callbackmanager = callbackmanager.factory.create(); setcontentview(r.layout.activity_main); loginbutton = (loginbutton) findviewbyid(r.id.login_button); loginbutton.setreadpermissions("email", "user_likes", "user_friends"); loginbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //callback registration loginmanager.getinstance().registercallback(callbackmanager, new facebookcallback<loginresult>() { @override public void onsuccess(loginresult loginresult) { // app code loginmanager.getinstance().loginwithreadpermissions(mainactivity.this, arrays.aslist("public_profile", "user_friends")); log.e("-->", arrays.aslist("public_profile", "user_friends").tostring()); toast.maketext(getapplication(), "success", toast.length_short).show(); } @override public void oncancel() { // app code toast.maketext(getapplication(),"fail",toast.length_short).show(); } @override public void onerror(facebookexception exception) { // app code toast.maketext(getapplication(),"error",toast.length_short).show(); } }); } }); } @override protected void onactivityresult(final int requestcode, final int resultcode, final intent data) { super.onactivityresult(requestcode, resultcode, data); callbackmanager.onactivityresult(requestcode, resultcode, data); }
}
my activity_main.xml file follows:-
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <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="30dp" android:layout_marginbottom="30dp" />
u can customize button per requirement
Comments
Post a Comment