Login to Facebook using Dialog in Android

How to implement this login using facebook? I followed this tutorial https://www.androidlearning.in/facebook-login-for-android-app/ but it generates full activity (traditional way).

I have facebook app installed

In applications like Memrise, Bandlab, etc. Shows me what Dialog is but not in my application

Also I am trying with

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList([...]);

      

but doesn't work ...

I want to:

Imagen

My application Show me:

Imagen

+4


source to share


3 answers


I faced the same problem as you 30 minutes ago, but I used a custom Facebook button and the following code inside onCreate to call Facebook Login:

loginButton = (CircularProgressButton) findViewById(R.id.btn_fb);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "user_friends"));
            }
        });

        //Register a callback
        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(final LoginResult loginResult) {
                        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object,GraphResponse response) {
                                        try {
                                            nome = object.getString("name");
                                            email = object.getString("email");
                                            String idfb  = loginResult.getAccessToken().getUserId();
                                            logarFb(idfb, nome, email);

                                        } catch(JSONException ex) {
                                            ex.printStackTrace();
                                        }
                                    }
                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,email,gender, birthday");
                        request.setParameters(parameters);
                        request.executeAsync();
                    }

                    @Override
                    public void onCancel() {
                        //cancelled
                    }

                    @Override
                    public void onError(FacebookException exception) {
                     //error
                    }
                });

      

I changed it to a Facebook login button:

<com.facebook.login.widget.LoginButton
    android:id="@+id/btn_fb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="30dp" /> 

      



Now that my code looks now much cleaner:

private LoginButton loginButton;
private CallbackManager callbackManager;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    callbackManager = CallbackManager.Factory.create();

        loginButton = findViewById(R.id.btn_fb);
        loginButton.setReadPermissions("email");
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                getUserDetails(loginResult);
            }

            @Override
            public void onCancel() {
                funcoes.aviso(MainActivity.this,"Você cancelou o login",R.color.red500,3000, R.drawable.ic_triste);
            }

            @Override
            public void onError(FacebookException exception) {
                funcoes.dialogoMsg(MainActivity.this,"Há algo de errado com o login do Facebook :/");
            }
        });
}

 protected void getUserDetails(final LoginResult loginResult) {
        GraphRequest data_request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted( JSONObject object, GraphResponse response) {
                        try {
                            nome = object.getString("name");
                            email = object.getString("email");
                            String idfb  = loginResult.getAccessToken().getUserId();
                            logarFb(idfb, nome, email);

                        } catch(JSONException ex) {
                            ex.printStackTrace();
                        }
                    }

                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email,gender, birthday");
        data_request.setParameters(parameters);
        data_request.executeAsync();

    }

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

      

If you have the Facebook app installed, this should be enough so that instead of entering full screen mode, instead of entering full screen mode, Facebook login is enabled as a dialog box.

If you want it, follow this tutorial. If you forgot another setting: https://www.studytutorial.in/android-facebook-integration-and-login-tutorial

+1


source


The dialog will only appear if you have facebook app installed on your phone, otherwise you will get a web view. Try to install the app and login.



0


source


You need to remove the "user_birthday" permission if you specified in your permissions.

LoginManager.getInstance().logInWithReadPermissions(context,Arrays.asList("public_profile","email"));

How some permissions are needed for "Browse Apps"

0


source







All Articles