/** * Initialize button container */ private void initializeButtonContainer() { if (mIconPosition == POSITION_TOP || mIconPosition == POSITION_BOTTOM) { this.setOrientation(LinearLayout.VERTICAL); } else { this.setOrientation(LinearLayout.HORIZONTAL); } if (this.getLayoutParams() == null) { LayoutParams containerParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); this.setLayoutParams(containerParams); } this.setGravity(Gravity.CENTER); // disable click listeners for fix bug in this issue as: // https://github.com/medyo/Fancybuttons/issues/100 //this.setClickable(true); //this.setFocusable(true); if(mIconResource==null && mFontIcon==null && getPaddingLeft()==0 && getPaddingRight()==0 && getPaddingTop()==0 && getPaddingBottom()==0){ //fix for all version of androids and screens this.setPadding(20, 0, 20, 0); } }
/** * Set the capitalization of text * * @param textAllCaps : is text to be capitalized */ public void setTextAllCaps(boolean textAllCaps) { this.mTextAllCaps = textAllCaps; setText(mText); }
FancyButton facebookLoginBtn = new FancyButton(this); facebookLoginBtn.setText("Login with Facebook"); facebookLoginBtn.setBackgroundColor(Color.parseColor("#3b5998")); facebookLoginBtn.setFocusBackgroundColor(Color.parseColor("#5474b8")); facebookLoginBtn.setTextSize(17); facebookLoginBtn.setRadius(5); facebookLoginBtn.setIconResource("\uf082"); facebookLoginBtn.setIconPosition(FancyButton.POSITION_LEFT); facebookLoginBtn.setFontIconSize(30); FancyButton foursquareBtn = new FancyButton(this); foursquareBtn.setText("Check in"); foursquareBtn.setBackgroundColor(Color.parseColor("#0072b1")); foursquareBtn.setFocusBackgroundColor(Color.parseColor("#228fcb")); foursquareBtn.setTextSize(17); foursquareBtn.setRadius(5); foursquareBtn.setIconResource("\uf180"); foursquareBtn.setIconPosition(FancyButton.POSITION_TOP); foursquareBtn.setFontIconSize(30); FancyButton installBtn = new FancyButton(this); installBtn.setText("Google play install"); installBtn.setBackgroundColor(Color.parseColor("#a4c639")); installBtn.setFocusBackgroundColor(Color.parseColor("#bfe156")); installBtn.setTextSize(17); installBtn.setRadius(5); installBtn.setCustomTextFont(R.font.roboto_slab_bold); installBtn.setIconPadding(0, 30, 0, 0); installBtn.setEnabled(false);
initializeButtonContainer(); mTextView = setupTextView(); mIconView = setupIconView(); mFontIconView = setupFontIconView(); this.removeAllViews(); setupBackground(); this.addView(view);
applyRadius(defaultDrawable); defaultDrawable.setColor(getResources().getColor(android.R.color.transparent)); // Hollow Background } else { defaultDrawable.setColor(mDefaultBackgroundColor); applyRadius(focusDrawable); applyRadius(disabledDrawable); defaultDrawable.setStroke(mBorderWidth, mDisabledBorderColor); if (mGhost) { disabledDrawable.setColor(getResources().getColor(android.R.color.transparent)); this.setBackground(getRippleDrawable(defaultDrawable, focusDrawable, disabledDrawable)); applyRadius(drawable2); drawable2.setColor(getResources().getColor(android.R.color.transparent)); // No focus color } else { drawable2.setColor(mFocusBackgroundColor); this.setBackgroundDrawable(states); } else { this.setBackground(states);
/** * Setup Text View * * @return : TextView */ private TextView setupTextView() { if (mText == null) { mText = "Fancy Button"; } TextView textView = new TextView(mContext); textView.setText(mText); textView.setGravity(mDefaultTextGravity); textView.setTextColor(mEnabled ? mDefaultTextColor : mDisabledTextColor); textView.setTextSize(Utils.pxToSp(getContext(), mDefaultTextSize)); textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); if (!isInEditMode() && !mUseSystemFont) { textView.setTypeface(mTextTypeFace, textStyle); //we can pass null in first arg } return textView; }
/** * Set custom font for button Text * * @param fontId : Font id * Place your text fonts in font resources. * Eg. res/font/roboto.ttf or res/font/roboto.xml */ public void setCustomTextFont(@FontRes int fontId) { mTextTypeFace = ResourcesCompat.getFont(getContext(), fontId); if (mTextView == null) initializeFancyButton(); else mTextView.setTypeface(mTextTypeFace, textStyle); }
/** * Set the size of Text in sp * * @param textSize : Text Size */ public void setTextSize(int textSize) { this.mDefaultTextSize = Utils.spToPx(getContext(), textSize); if (mTextView != null) mTextView.setTextSize(textSize); }
if (!isInEditMode()) { fontIconView.setTextSize(Utils.pxToSp(getContext(), mFontIconSize)); fontIconView.setText(mFontIcon); fontIconView.setTypeface(mIconTypeFace); } else { fontIconView.setTextSize(Utils.pxToSp(getContext(), mFontIconSize)); fontIconView.setText("O");
/** * Set Icon size of the button (for only font icons) in sp * * @param iconSize : Icon Size */ public void setFontIconSize(int iconSize) { this.mFontIconSize = Utils.spToPx(getContext(), iconSize); if (mFontIconView != null) mFontIconView.setTextSize(iconSize); }
private Typeface getTypeface(TypedArray ta) { if (ta.hasValue(R.styleable.FancyButtonsAttrs_android_fontFamily)) { int fontId = ta.getResourceId(R.styleable.FancyButtonsAttrs_android_fontFamily, 0); if (fontId != 0) return ResourcesCompat.getFont(getContext(), fontId); } if(ta.hasValue(R.styleable.FancyButtonsAttrs_fb_textFontRes)){ int fontId = ta.getResourceId(R.styleable.FancyButtonsAttrs_fb_textFontRes, 0); if (fontId != 0) return ResourcesCompat.getFont(getContext(), fontId); } return null; }