0

I am integrating facebook login in my android app in which I want when a simple button is click then I want to fetch user details.I do not want to place facebook login button. How can I do that

here is my code:-

private Button loginButton;
private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    setContentView(R.layout.activity_main);

    loginButton = (Button) findViewById(R.id.login_button);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile, email, user_birthday, user_friends"));
        }
    });
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    Log.d("Success", "Login");
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    Log.e("JSON:", object.toString());
                                    String user_ID = null;
                                    try {
                                        user_ID = object.getString("id");
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    CFacbookStorage facebookstorage = new CFacbookStorage();
                                    facebookstorage.setM_UserId(user_ID);
                                    Intent i = new Intent(MainActivity.this, CFacebookDeatails.class);
                                    i.putExtra("user_data", facebookstorage);
                                    startActivity(i);
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email,gender, birthday");
                    request.setParameters(parameters);
                    request.executeAsync();

                }

                @Override
                public void onCancel() {
                    // Toast.makeText(Login.this, "Login Cancel", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onError(FacebookException exception) {
                    Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    AccessToken token;
    token = AccessToken.getCurrentAccessToken();


    if (token == null) {
        //Means user is not logged in
    } else {
        Log.d("TAG", "init: facebooklogin   " + AccessToken.USER_ID_KEY + "   " + AccessToken.ACCESS_TOKEN_KEY + "  ");
    }
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile, email, user_birthday, user_friends"));

        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

}

CFacbookStorage class code:-

    public static CFacbookStorage cFacbookStorage;

public String getM_UserId() {
    return m_UserId;
}

public void setM_UserId(String m_UserId) {
    this.m_UserId = m_UserId;
}

public String m_UserId;

public static CFacbookStorage getInstance(){
    if (cFacbookStorage==null){
        cFacbookStorage = new CFacbookStorage();
    }
    return cFacbookStorage;
}

}

CFacebookDetail

private TextView info;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_details);
    info = (TextView)findViewById(R.id.info);

}

in this code I am getting error in loginButton.registerCallback(){}

Sandeep
  • 13
  • 4
  • You mean you want facebook login to work at one go ?? – Moulesh Sep 02 '16 at 04:18
  • yes ...................................................... – Sandeep Sep 02 '16 at 04:19
  • If you are fetching information from facebook... And dont want to show facebook login instead show some different button then you are ethically not right... And facebook do not let you fetch the information unless the user agrees to allow your app to access their basic information... Atleast then user will get to know that information is being fetched from fb.. So single click and fetch profile information is never possible. – Moulesh Sep 02 '16 at 04:26

2 Answers2

1

used your button in xml file then after in oncreate of activity use below code:

private CallbackManager callbackManager;

callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(final LoginResult loginResult) {
                        Log.d("Success", "Login");
                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {
                                        // Application code
                                        response.getError();
                                        Log.e("JSON:", object.toString());
                                    }
                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,email,gender, birthday");
                        request.setParameters(parameters);
                        request.executeAsync();

                    }

                    @Override
                    public void onCancel() {
                        // Toast.makeText(Login.this, "Login Cancel", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Toast.makeText(SignInActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
        AccessToken token;
        token = AccessToken.getCurrentAccessToken();


        if (token == null) {
            //Means user is not logged in
        } else {
            Log.d("TAG", "init: facebooklogin   " + AccessToken.USER_ID_KEY + "   " + AccessToken.ACCESS_TOKEN_KEY + "  ");
        }

after add below code on button click event:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    LoginManager.getInstance().logInWithReadPermissions(SignInActivity.this, Arrays.asList("public_profile, email, user_birthday, user_friends"));

            }
        });

the put in that activity

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
sneha desai
  • 886
  • 1
  • 11
  • 19
  • String user_id=object.getString("id").toString(); String email=object.getString("email").toString(); String name=object.getString("name").toString(); String gender =object.getString("gender").toString(); – sneha desai Sep 02 '16 at 04:53
  • I am like that:- public void onCompleted(JSONObject object, GraphResponse response) { try { String user_Id = object.getString("id").toString(); CFacbookStorage.getInstance().setM_UserId(user_Id); Intent i = new Intent(MainActivity.this,CFacebookDeatails.class); startActivity(i); } catch (JSONException e) { e.printStackTrace(); } // Application code response.getError(); Log.e("JSON:", object.toString()); } }); – Sandeep Sep 02 '16 at 05:04
  • is this correct I am setting user data in Storage class and then navigating to another activity where I want to show user data – Sandeep Sep 02 '16 at 05:04
  • are you pass that user data with intent? – sneha desai Sep 02 '16 at 05:08
  • no first I set user data in Storage class then navigate to another class and in that class I retreive data from storage class – Sandeep Sep 02 '16 at 05:19
  • you want user data in CFacebookDetails activity? – sneha desai Sep 02 '16 at 05:22
  • first in CFacbookStorage class implement Serializable interface then CFacbookStorage facebookstorage=new CFacbookStorage(); facebookstorage.setM_UserId(user_ID); Intent i = new Intent(MainActivity.this,CFacebookDeatails.class); i.putExtra("user_data", facebookstorage); startActivity(i); in CFacebookDeatails activity CFacbookStorage facebookstorage=getIntent().getSerializableExtra("user_data"); here in CFacbookStorage store all user data. also see this link for refe.http://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android – sneha desai Sep 02 '16 at 05:31
  • but it does not fetch any user data – Sandeep Sep 02 '16 at 05:39
  • give me your hole code with activity and object class – sneha desai Sep 02 '16 at 05:44
  • where is your CFacbookStorage class and CFacebookDeatails activity? – sneha desai Sep 02 '16 at 05:46
  • CFacbookStorage facebookstorage=getIntent().getSerializableExtra("user_data"); add this line in CFacebookDeatails activity and do public class CFacbookStorage **implement Serializable** – sneha desai Sep 02 '16 at 05:54
  • 09-02 11:26:32.109 12296-12296/com.example.devui1.socialintegration D/TAG: init: facebooklogin user_id access_token – Sandeep Sep 02 '16 at 05:58
  • then use this for get user id and save into CFacbookStorage object – sneha desai Sep 02 '16 at 06:03
  • but i am not getting any value in Log.e("JSON:", response.toString()); – Sandeep Sep 02 '16 at 06:06
  • when I user Facebook login Button then I retrieve user information successfull ,,,, – Sandeep Sep 02 '16 at 06:13
  • by doing this way I don;t – Sandeep Sep 02 '16 at 06:13
  • in above code you put imagebutton onclick event two time remove any one from that – sneha desai Sep 02 '16 at 06:19
0

You can achieve this by overriding you facebook button view with that of your appcompat button using FrameLayout. Something like this :

           <FrameLayout
                android:id="@+id/FrameLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

            <com.facebook.login.widget.LoginButton
                android:id="@+id/facebook"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@drawable/facebook"
                android:layout_gravity="center_horizontal"/>


            <ImageView
                android:id="@+id/fb_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/facebook"
                android:onClick="onClick"
                android:layout_marginTop="10dp"
                android:layout_gravity="center_horizontal"/>

            </FrameLayout>

onClick Implementation :

 public void onClick(View v) {
        if (v == custom_fb_login) {
            fb_login.performClick();
        }
    }

FbButton Click:

 private View.OnClickListener handleClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
             case R.id.facebook:
                    LoginManager.getInstance().logInWithReadPermissions(Login.this, Arrays.asList("public_profile", "email"));
                    break;
                    }
                    }
                }
Nidhi
  • 777
  • 7
  • 17
  • see here problem is that I dont want facebook button text change to logout always remain lig in – Sandeep Sep 02 '16 at 05:17
  • You should now worry for that as facebook's original button will get hidden by your own facebook button image as you will be using a framelayout. Have you tried executing my xml code? – Nidhi Sep 02 '16 at 05:24