34

Facebook Android sdk has a com.facebook.widget.LoginButton

I want to put my own image for the Login button. Is it possible ?

So far i've tried adding android:src="@drawable/facebook" to the layout file as an attribute to the button element with no luck

Michael
  • 22,196
  • 33
  • 132
  • 187

4 Answers4

78

I ended up overriding the text to be empty string and then defining the setBackgroundResource of the button to my image (didn't need the dynamic login/logout text functionality)

<com.facebook.widget.LoginButton
        xmlns:fb="http://schemas.android.com/apk/res-auto"
        android:id="@+id/login_button"
        android:layout_width="249dp"
        android:layout_height="45dp"
        android:layout_above="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="30dp"
        android:layout_marginTop="30dp"
        android:contentDescription="@string/login_desc"
        android:scaleType="centerInside"
        fb:login_text=""
        fb:logout_text="" />

And in code I defined the background resource :

final LoginButton button = (LoginButton) findViewById(R.id.login_button);
button.setBackgroundResource(R.drawable.facebook);

Kind of a workaround, but I preferred this over changing Facebook SDK code (although it's very straight forward as well) and worry about updating each time I update the their version.

Michael
  • 22,196
  • 33
  • 132
  • 187
  • 1
    Just curious: where did you even find the documentation/help about the `fb:login_text` and such XML attributes? It's exactly what I was looking for but have no idea how people stumbled upon it. – Tony Chan Jul 24 '13 at 00:40
  • 3
    Facebook sdk is open source. Check its res/values/attrs.xml – Dmitriy Voronin Jul 24 '13 at 11:18
  • 25
    add button.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); to remove that 'f' icon of Facebook. – Krrishnaaaa Feb 04 '14 at 12:59
  • @user3740085 on your view xml containing the fb widget, in the fb widget element (``) add attribute `fb:logout_text="logout text"` – Michael Jul 17 '14 at 14:34
  • 3
    New updated facebook sdk in android-studio- i am getting no resource found error fb:login_text="" fb:logout_text="" build time same code but getting above 2 error – Sanket990 Jul 30 '15 at 05:17
  • 2
    To changed text facebook:com_facebook_login_text="New text" https://github.com/facebook/facebook-android-sdk/blob/master/facebook/res/values/attrs.xml – Tigran Sarkisian Nov 04 '15 at 10:53
10

yes if you want to change text and image both then write the below code.

authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setBackgroundResource(R.drawable.icon);
authButton.setText("Login");
authButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);`
Jerodev
  • 32,252
  • 11
  • 87
  • 108
Heena M. Patel
  • 144
  • 1
  • 7
2

An other way

loginButton = (LoginButton) findViewById(R.id.fb_login_button);
loginButton.setVisibility(View.GONE);




ImageView ivFbCustomButton = (ImageView) findViewById(R.id.iv_fb_custom_button);
ivFbCustomButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        com.facebook.login.widget.LoginButton btn = new com.facebook.login.widget.LoginButton(FacebookActivity.this);
        btn.performClick();
    }
});

Note:

You have to write the code for two buttons in XML file. One is for default facebook button (We are hiding it at initial step). Second one is for custom button

Bahu
  • 1,516
  • 2
  • 28
  • 49
0

Create a drawable with name com_facebook_button_icon.xml add anything inside it FB login button will override as it uses as drawableleft.

For example:

<vector
android:height="25dp"
android:viewportHeight="1365.3333"
android:viewportWidth="1365.3333"
android:width="25dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:tintMode="multiply"
android:tint="@color/com_facebook_button_text_color"
>
<path
    android:fillAlpha="1"
    android:fillColor="#FFFFFF"
    android:fillType="nonZero"
    android:pathData="m1365.33,682.67c0,-377.03 -305.64,-682.67 -682.67,-682.67C305.64,-0 0,
        305.64 0,682.67 0,1023.41 249.64,1305.83 576,1357.04L576,880L402.67,880l0,
        -197.33l173.33,-0l0,-150.4c0,-171.09 101.92,-265.6 257.85,-265.6 74.69,-0 152.81,
        13.33 152.81,13.33L986.67,448L900.58,448C815.78,448 789.33,500.62 789.33,
        554.61L789.33,682.67L978.67,682.67L948.4,880L789.33,880L789.33,1357.04C1115.69,
        1305.83 1365.33,1023.41 1365.33,682.67"
    android:strokeColor="#00000000"
    />
GTID
  • 538
  • 2
  • 6
  • 19