Custom Facebook Login Button - Android -


i''m following tutorial far can't make work, though year ago or so...

i'm using androidstudo 1.2.2 , facebooksdk 4.

i want simple login facebook using custom button, 1 shown in image:

example

now, in example tutorial i'm having problems session variable, says cannot resolve it, neither getactivity()

has naybody tried on facebooksdk4.0?

is correct approach or maybe there more updated?

thanks in advance!

step 1: first add framelayout , make facebook button visibility="gone" , add custom button. don't forgot put xmlns:facebook="http://schemas.android.com/apk/res-auto" in main layout.

<framelayout         android:id="@+id/framelayout1"         android:layout_width="match_parent"         android:layout_height="wrap_content" >          <com.facebook.login.widget.loginbutton             android:id="@+id/login_button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:visibility="gone" />          <button             android:id="@+id/fb"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:background="#416bc1"             android:onclick="onclick"             android:text="facebook"             android:textcolor="#ffffff"             android:textstyle="bold" />     </framelayout> 

step 2: initialize facebooksdk in oncreate before inflecting layout.

facebooksdk.sdkinitialize(this.getapplicationcontext());

step 3: add java file.

callbackmanager = callbackmanager.factory.create();  fb = (button) findviewbyid(r.id.fb); loginbutton = (loginbutton) findviewbyid(r.id.login_button);  list < string > permissionneeds = arrays.aslist("user_photos", "email",     "user_birthday", "public_profile", "accesstoken"); loginbutton.registercallback(callbackmanager, new facebookcallback < loginresult > () {@override     public void onsuccess(loginresult loginresult) {          system.out.println("onsuccess");          string accesstoken = loginresult.getaccesstoken()             .gettoken();         log.i("accesstoken", accesstoken);          graphrequest request = graphrequest.newmerequest(         loginresult.getaccesstoken(),         new graphrequest.graphjsonobjectcallback() {@override             public void oncompleted(jsonobject object,             graphresponse response) {                  log.i("loginactivity",                 response.tostring());                 try {                     id = object.getstring("id");                     try {                         url profile_pic = new url(                             "http://graph.facebook.com/" + id + "/picture?type=large");                         log.i("profile_pic",                         profile_pic + "");                      } catch (malformedurlexception e) {                         e.printstacktrace();                     }                     name = object.getstring("name");                     email = object.getstring("email");                     gender = object.getstring("gender");                     birthday = object.getstring("birthday");                 } catch (jsonexception e) {                     e.printstacktrace();                 }             }         });         bundle parameters = new bundle();         parameters.putstring("fields",             "id,name,email,gender, birthday");         request.setparameters(parameters);         request.executeasync();     }      @override     public void oncancel() {         system.out.println("oncancel");     }      @override     public void onerror(facebookexception exception) {         system.out.println("onerror");         log.v("loginactivity", exception.getcause().tostring());     } }); 

step 4: don't forget add following code.

@override protected void onactivityresult(int requestcode, int responsecode, intent data) {     super.onactivityresult(requestcode, responsecode, data);     callbackmanager.onactivityresult(requestcode, responsecode, data); } 

step 5: set custom button click facebooklogin button click.

public void onclick(view v) {     if (v == fb) {         loginbutton.performclick();     }  } 

step 6: programmatically logout use this.

loginmanager.getinstance().logout();

step 7: can find user logged in or not profile.

profile = profile.getcurrentprofile().getcurrentprofile(); if (profile != null) {     // user has logged in } else {     // user has not logged in } 

Comments