Android : How to set our own background image to facebook Login Button in xml?

on Thursday, August 7, 2014


is it possible to set our own background image in xml for Facebook Login Button ?


my xmlc in my Project(i.e FBDemo)




<com.facebook.widget.LoginButton
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:id="@+id/btn_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_login"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
facebook:background_image="@drawable/login_with_fb"
facebook:confirm_logout="false"
facebook:fetch_user_info="true" />


I make changes in Facebook SDK like this


in FacebookSDK project values/attr.xml




<declare-styleable name="com_facebook_login_view">
<attr name="confirm_logout" format="boolean"/>
<attr name="fetch_user_info" format="boolean"/>
<attr name="login_text" format="string"/>
<attr name="logout_text" format="string"/>
<attr name="background_image" format="integer"/>
</declare-styleable>


and in LoginButton.java



private int background_image;
private void parseAttributes(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.com_facebook_login_view);
confirmLogout = a.getBoolean(R.styleable.com_facebook_login_view_confirm_logout, true);
fetchUserInfo = a.getBoolean(R.styleable.com_facebook_login_view_fetch_user_info, true);
loginText = a.getString(R.styleable.com_facebook_login_view_login_text);
logoutText = a.getString(R.styleable.com_facebook_login_view_logout_text);
background_image = a.getInt(R.styleable.com_facebook_login_view_background_image, R.drawable.com_facebook_button_blue);

a.recycle();
}

public LoginButton(Context context, AttributeSet attrs) {
super(context, attrs);

if (attrs.getStyleAttribute() == 0) {
// apparently there's no method of setting a default style in xml,
// so in case the users do not explicitly specify a style, we need
// to use sensible defaults.
this.setGravity(Gravity.CENTER);
this.setTextColor(getResources().getColor(R.color.com_facebook_loginview_text_color));
this.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.com_facebook_loginview_text_size));
this.setTypeface(Typeface.DEFAULT_BOLD);
if (isInEditMode()) {
// cannot use a drawable in edit mode, so setting the background color instead
// of a background resource.
this.setBackgroundColor(getResources().getColor(R.color.com_facebook_blue));
// hardcoding in edit mode as getResources().getString() doesn't seem to work in IntelliJ
loginText = "Log in with Facebook";
} else {
//this.setBackgroundResource(R.drawable.com_facebook_button_blue);
this.setBackgroundResource(background_image);
this.setCompoundDrawablesWithIntrinsicBounds(R.drawable.com_facebook_inverse_icon, 0, 0, 0);
this.setCompoundDrawablePadding(
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_compound_drawable_padding));
this.setPadding(getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_left),
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_top),
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_right),
getResources().getDimensionPixelSize(R.dimen.com_facebook_loginview_padding_bottom));
}
}
parseAttributes(attrs);
if (!isInEditMode()) {
initializeActiveSessionWithCachedToken(context);
}
}


I am passing my own background to xml of Facebbok loginbutton in my xml


but this gives me runtime error like below



08-07 06:41:59.492: E/AndroidRuntime(1731): Caused by: java.lang.NumberFormatException: Invalid int: "res/drawable-hdpi/login_with_fb.png"


I think this error occur because of login_with_fb.png image is reside in FBDemo and FacebookSDK project can't find drawable image login_with_fb.png in it


I know the other way, just setting background image in Activity , but I want to set in xml is there any solution ??


0 comments:

Post a Comment