0

Using facebook sdk 4.6.0 i am trying to use facebook login integration in my android app. com.facebook.login.widget.LoginButton shows following button enter image description here

I want to change text from "Login with Facebook" to "FACEBOOK" only

I have tried almost all solutions provided previously, but none of them worked for me. Probable reason i could think of is that all previously answered question are for facebook sdk 3.0 which seems to fails for facebook sdk 4.6.0

I tried:

  1. modifying LoginButton Class but it is locked

  2. xmlns:facebook="http://schemas.android.com/apk/res-auto" using this schema in LoginButton and changing text using facbook:login_text="LOGIN", facebook:com_facebook_login_text="LOGIN" but both failed to work

This is my dependencies for facebook sdk compile 'com.facebook.android:facebook-android-sdk:4.6.0'

This is my LoginButton xml

<com.facebook.login.widget.LoginButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/buttonRegisterFacebook"
    android:padding="10dp"
   />

Please suggest some way to change text and change fb logo image size

Edit : Now i understand why none of the previously stated method is working for me. My Facebook SDK files are locked

LoginButton.java file is locked

I tried unlocking by clicking on lock button at bottum right corner in android studio but nothing happens. Any suggestions how to unlock FB SDK files. My FB SDK version is 4.6.0

Aditya Verma
  • 91
  • 1
  • 10
  • Plz see this if not already found http://stackoverflow.com/questions/16314651/customize-android-facebook-login-button – VVB Jan 17 '16 at 06:43

2 Answers2

0

Please change here in the code of LoginButton.java class of facebook SDK

protected void configureButton(
        final Context context,
        final AttributeSet attrs,
        final int defStyleAttr,
        final int defStyleRes) {
    super.configureButton(context, attrs, defStyleAttr, defStyleRes);
    setInternalOnClickListener(new LoginClickListener());

    parseLoginButtonAttributes(context, attrs, defStyleAttr, defStyleRes);

    if (isInEditMode()) {
        // cannot use a drawable in edit mode, so setting the background color instead
        // of a background resource.
        setBackgroundColor(getResources().getColor(R.color.com_facebook_blue));
        // hardcoding in edit mode as getResources().getString() doesn't seem to work in
        // IntelliJ
        loginText = "Facebook";
    } else {
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(
                    AccessToken oldAccessToken,
                    AccessToken currentAccessToken) {
                setButtonText();
            }
        };
    }

    setButtonText();
}
0

After searching a lot, i found out for the recent Facebook SDK 4.6.0, inorder to only change FB Login Button text, add this to res/values/strings.xml

<string name="com_facebook_loginview_log_out_button">Your Logout Text</string>
<string name="com_facebook_loginview_log_in_button_long">Your Login Text</string>

However, if we want to have a FB Login button functionality in any random button

add following lines to ClickListener of random button ('buttonLogin' in my case )

buttonLogin.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           LoginButton loginButton = new LoginButton(Login.this);
           AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
               @Override
               protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
                   updateWithToken(newAccessToken);
               }
           };

           CallbackManager callbackManager = CallbackManager.Factory.create();
           loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
           loginButton.registerCallback(callbackManager, callback);
           loginButton.performClick();
}

I only required users public profile and email

updateWithToken() goes as

private void updateWithToken( AccessToken currentAccessToken ){
    if(currentAccessToken!=null){
        Log.v("MyApp", getClass().toString() + "updateWithToken:If(Token NonNull)");

        GraphRequest request = GraphRequest.newMeRequest(currentAccessToken, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {
                Log.v("MyApp",getClass().toString() + response.toString());
                // Get facebook data from login
                try {
                    Log.v("MyApp", getClass().toString() + object.toString()); // contains data string
                } catch (JSONException e) {
                    Log.v("MyApp", getClass().toString() + "LoginJSON");
                    e.printStackTrace();
                }
                Intent intent = new Intent(Login.this, Home.class); // once data is received i wanted to open Home Activity
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }
        });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location");
        request.setParameters(parameters);
        request.executeAsync();
    } else {
        Log.v("MyApp", getClass().toString() + "updateWithToken:Else(Token Null)");
    }
}//updatewithtoken

Also Override OnActivityResult as

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.v("MyApp", getClass().toString() + " Request:" + requestCode + " Result:" + resultCode);
    if (requestCode==64206) {
        Log.v("MyApp", getClass().toString() + " onActivityResult:If Facebook");
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

I used log to find request code is 64206 for facebook login.

Aditya Verma
  • 91
  • 1
  • 10