简体   繁体   中英

Android: How to hide progress circle in Facebook login

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

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.

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.

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());
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM