17

I am using this method to perform a Facebook login without using the fb button Facebook authentication without login button

It's working fine, but a progress bar with black background is shown during fb login, I guess from activity com.facebook.LoginActivity

How can I avoid displaying that activity?, I just want to show my own progress from my app activity during login in com.facebook.LoginActivity

Community
  • 1
  • 1
jmhostalet
  • 4,399
  • 4
  • 38
  • 47

3 Answers3

63

I had the same problem with facebook sdk 4.x. When I click the facebook login button the Facebook Activity appears translucent but it shows a progress bar. Luckily we can disable this progress bar in the theme. So the Facebook Activity is declared as

<activity
    android:name="com.facebook.FacebookActivity"
    android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

All we have to do is create a style that inherits from Theme.Translucent.NoTitleBar and hides the progress bar:

<style name="FullyTranslucent" parent="android:Theme.Translucent.NoTitleBar">
    <item name="android:progressBarStyle">@style/InvisibleProgress</item>
</style>

<style name="InvisibleProgress">
    <item name="android:visibility">gone</item>
</style>

Now set the theme of the activity to our new theme:

<activity
    android:name="com.facebook.FacebookActivity"
    android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
    android:label="@string/app_name"
    android:theme="@style/FullyTranslucent" />

Voila! The ProgressBar before login is gone.

VM4
  • 6,321
  • 5
  • 37
  • 51
  • @Andrew just tried this with 4.7 and it worked fine. Are you still having trouble with this? – VM4 Nov 30 '15 at 12:28
  • Yes. Doesn't work at all. I'm not even sure how it works for you, considering they are building the ProgressDialog programatically: spinner = new ProgressDialog(getContext()); – Bogdan Zurac Dec 01 '15 at 14:19
  • Use android:theme="@style/FullyTranslucent" instead of android:theme="@android:style/Theme.Translucent.NoTitleBar" in your Manifest. It works. Thank you – Michael Katkov Feb 14 '16 at 14:46
  • 6
    Worked fine, i added " tools:replace="android:theme" " to activity for some conflicting issues but thanks! – Andrea Biagioni Jul 21 '16 at 13:17
  • Where should place ` – János Sep 28 '16 at 09:19
  • I'm getting a "No resource found that matches the given name (at 'android:progressBarStyle' with value '@style/InvisibleProgress')". –  Aug 14 '18 at 15:23
  • I make it work after adding `tools:replace="android:theme"` in activity and `xmlns:tools="http://schemas.android.com/tools"` in the manifest – Snowbases May 25 '21 at 09:15
21

To further @VM4's excellent answer I modified their approach for it to work correctly with SDK version 4.12.0

Firstly I added the following to AndroidManifest.xml

 <activity xmlns:tools="http://schemas.android.com/tools"
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@style/Translucent"
            tools:replace="android:theme"/>

In Android Studio 2.2, it is likely that Manifest Merging may produce an error complaining that the android:theme cannot be overridden as it already exists. This can be resolved using tools:replace="android:theme" in the <activity> tag.

I created a custom style within /res/values/styles.xml

 <style name="Translucent" parent="Translucent.Base"/>

 <style name="Translucent.Base" parent="android:Theme.Translucent.NoTitleBar">
    <item name="android:progressBarStyle">@style/InvisibleProgress</item>
 </style>

This correctly removed the hideous Facebook progress dialog.

However, on 5.0 (API 21)+ devices this did have the side effect of coloring the top most system bar black for the time the FacebookActivity was active.

To fix this I added a style in res/values-v21/styles.xml

<style name="Translucent" parent="Translucent.Base">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style> 

This made the theme completely transparent and removed the progress dialog.

Finally, one thing to note with solutions that recommend using @android:style/Theme.NoDisplay is that this will not work on Android Marshmallow 6.0 (API 23)+ and should probably be avoided in future.

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
-1

Simple solution just show progressbar in registercallback

See my code

fb_login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {

            progressBar.setVisibility(View.VISIBLE);

            // App code
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            // Application code
                            Log.v("Profile ---------   ", response.toString());

                            progressBar.setVisibility(View.GONE);

                            try {

                                if (object!=null){

                                    F_ID = object.getString("id");
                                    if (object.has("first_name"))
                                        Name = object.getString("name");
                                    Log.d(TAG, "onCompleted: Name - "+object.getString("name"));
                                    if (object.has("last_name"))
                                        LastName = object.optString("last_name");
                                    Log.d(TAG, "onCompleted: LastName - "+object.optString("last_name"));
                                    if (object.has("email"))
                                        Email = object.optString("email");
                                    if (object.has("birthday"))
                                        DOB = object.optString("birthday");


                                    ProfilePic = "https://graph.facebook.com/" + F_ID + "/picture?type=large";

                                    Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show();

                                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                    intent.putExtra("Name", object.getString("name"));
                                    intent.putExtra("Email", Email);
                                    intent.putExtra("DOB", DOB);
                                    intent.putExtra("ID", F_ID);
                                    intent.putExtra("ImgURL", ProfilePic);
                                    Log.d(TAG, "onCompleted: Email = "+Email+" Name = "+Name+" FID = "+F_ID);
                                    //sharedpreference is used to store the email, password and the useername
                                    SharedPreferenceManager.setDefaults("email", Email, SigninActivity.this);
                                    SharedPreferenceManager.setDefaults("facebook_id", F_ID, SigninActivity.this);
                                    SharedPreferenceManager.setDefaults("profile_pic", "https://graph.facebook.com/" + F_ID + "/picture?type=large", SigninActivity.this);

                                    if (object.has("name"))
                                    SharedPreferenceManager.setDefaults("username", Name, SigninActivity.this);
                                    Log.d(TAG, "onCompleted: Store shared data");

                                    startActivity(intent);

                                }else
                                    Log.d(TAG, "onCompleted: object is null "+object);


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

                        }
                    });

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

            System.out.println("Facebook Login Successful!");
            System.out.println("Logged in user Details : ");
            System.out.println("--------------------------");
            System.out.println("User ID  : " + loginResult.getAccessToken().getUserId());
            System.out.println("Authentication Token : " + loginResult.getAccessToken().getToken());

        }

        @Override
        public void onCancel() {
            Toast.makeText(getApplicationContext(), "Login cancelled by user!", Toast.LENGTH_LONG).show();
            System.out.println("Facebook Login Cancel!!");

        }

        @Override
        public void onError(FacebookException e) {
            Toast.makeText(getApplicationContext(), "Something went wrong!!", Toast.LENGTH_LONG).show();
            System.out.println("Facebook Login failed!! because of " + e.getCause().toString());
        }
    });
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
  • The Facebook SDK has it's own "layer" and it shows their spinner over your view. Where in your answer is the HIDING part which user asked for? You just copied bunch of login stuff from your app and added the ProgressBar over it... – Bartando Feb 14 '18 at 16:39