Skip to content

Commit 79574f1

Browse files
authored
Merge pull request #118 from vkrissz/feature-add-font-resource-support
Add support for font resources.
2 parents 495b867 + 88e90fd commit 79574f1

File tree

12 files changed

+140
-36
lines changed

12 files changed

+140
-36
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Icons, Borders, Radius ... for Android buttons
2121

2222
### Changelog
2323

24+
- 1.9.0
25+
- Add support for font resources (For text font only)
26+
- Increase min API level to 14
27+
- Now you can use these to set text font (res/font/roboto_slab_bold.ttf):
28+
android:fontFamily="@font/roboto_slab_bold"
29+
fancy:fb_textFontRes="@font/roboto_slab_bold"
30+
- Add dependency on support library used only when using font resources.
31+
2432
- 1.8.4
2533
- Fix Text Gravity
2634
- Add Ability to define custom radius value for each corner
@@ -55,7 +63,11 @@ Icons, Borders, Radius ... for Android buttons
5563

5664
### Installation
5765

58-
compile 'com.github.medyo:fancybuttons:1.8.4'
66+
implementation 'com.github.medyo:fancybuttons:1.9.0'
67+
68+
### To use font resources add support library to your dependencies:
69+
70+
implementation "com.android.support:appcompat-v7:$support_lib_version"
5971

6072
### Usage
6173

@@ -78,6 +90,7 @@ Icons, Borders, Radius ... for Android buttons
7890
| fancy:fb_textColor | setTextColor(int) | Text Color of the button |
7991
| fancy:fb_textSize | setTextSize(int) | Size of the text |
8092
| fancy:fb_textFont | setCustomTextFont(String) | FontFamily of the text|
93+
| fancy:fb_textFontRes | setCustomTextFont(int) | FontFamily of the text using font resource. REQUIRES support library|
8194
| fancy:fb_textGravity | setTextGravity(Int) | Gravity of the text|
8295
| fancy:fb_iconResource | setIconResource(Drawable) | Drawable icon of the button|
8396
| fancy:fb_iconPosition | setIconPosition(int) | Position of the icon : Left, Right, Top, Bottom|
@@ -110,6 +123,7 @@ Default Attributes have more priority than Attributes with prefix fancy.
110123
| android:text |
111124
| android:textSize |
112125
| android:textAllCaps |
126+
| android:fontFamily |
113127

114128
#### Supported Getters
115129
| Function | Description |
@@ -174,6 +188,7 @@ Fancybuttons is delivered with :
174188

175189
**How to add new fonts ?**
176190
Just Paste your font inside `assets/fonts/` folder for Text fonts or inside `assets/iconfonts/` for icon fonts eg : entypo
191+
OR for text fonts add it to res/font/ and use android:fontFamily or fancy:fb_textFontRes to use it.
177192

178193
## Contributions
179194
Fancybuttons needs you to build the missing features :

build.gradle

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4+
ext.support_lib_version = '27.1.1'
45
repositories {
56
jcenter()
7+
google()
68
}
79
dependencies {
8-
classpath 'com.android.tools.build:gradle:3.0.1'
10+
classpath 'com.android.tools.build:gradle:3.2.0-beta05'
911
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
1012
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
1113
}
@@ -14,5 +16,6 @@ buildscript {
1416
allprojects {
1517
repositories {
1618
jcenter()
19+
google()
1720
}
1821
}

fancybuttons_library/build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ android {
2020
}
2121

2222
dependencies {
23-
compile fileTree(dir: 'libs', include: ['*.jar'])
23+
implementation fileTree(dir: 'libs', include: ['*.jar'])
24+
implementation "com.android.support:support-annotations:$support_lib_version"
25+
api "com.android.support:appcompat-v7:$support_lib_version"
2426
}
2527

2628
archivesBaseName = 'fancybuttons'

fancybuttons_library/src/main/java/mehdi/sakout/fancybuttons/FancyButton.java

+40-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import android.graphics.drawable.RippleDrawable;
1414
import android.graphics.drawable.StateListDrawable;
1515
import android.os.Build;
16+
import android.support.annotation.FontRes;
17+
import android.support.v4.content.res.ResourcesCompat;
1618
import android.util.AttributeSet;
1719
import android.view.Gravity;
1820
import android.view.View;
@@ -360,12 +362,31 @@ private void initAttributesArray(TypedArray attrsArray) {
360362
? Utils.findFont(mContext, iconFontFamily, mDefaultIconFont)
361363
: Utils.findFont(mContext, mDefaultIconFont, null);
362364

363-
mTextTypeFace = textFontFamily != null
364-
? Utils.findFont(mContext, textFontFamily, mDefaultTextFont)
365-
: Utils.findFont(mContext, mDefaultTextFont, null);
365+
Typeface fontResource = getTypeface(attrsArray);
366+
if (fontResource != null) {
367+
mTextTypeFace = fontResource;
368+
} else {
369+
mTextTypeFace = textFontFamily != null
370+
? Utils.findFont(mContext, textFontFamily, mDefaultTextFont)
371+
: Utils.findFont(mContext, mDefaultTextFont, null);
372+
}
366373
}
367374
}
368375

376+
private Typeface getTypeface(TypedArray ta) {
377+
if (ta.hasValue(R.styleable.FancyButtonsAttrs_android_fontFamily)) {
378+
int fontId = ta.getResourceId(R.styleable.FancyButtonsAttrs_android_fontFamily, 0);
379+
if (fontId != 0)
380+
return ResourcesCompat.getFont(getContext(), fontId);
381+
}
382+
if(ta.hasValue(R.styleable.FancyButtonsAttrs_fb_textFontRes)){
383+
int fontId = ta.getResourceId(R.styleable.FancyButtonsAttrs_fb_textFontRes, 0);
384+
if (fontId != 0)
385+
return ResourcesCompat.getFont(getContext(), fontId);
386+
}
387+
return null;
388+
}
389+
369390
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
370391
private Drawable getRippleDrawable(Drawable defaultDrawable, Drawable focusDrawable, Drawable disabledDrawable) {
371392
if (!mEnabled) {
@@ -807,6 +828,22 @@ public void setCustomTextFont(String fontName) {
807828
mTextView.setTypeface(mTextTypeFace, textStyle);
808829
}
809830

831+
/**
832+
* Set custom font for button Text
833+
*
834+
* @param fontId : Font id
835+
* Place your text fonts in font resources.
836+
* Eg. res/font/roboto.ttf or res/font/roboto.xml
837+
*/
838+
public void setCustomTextFont(@FontRes int fontId) {
839+
mTextTypeFace = ResourcesCompat.getFont(getContext(), fontId);
840+
841+
if (mTextView == null)
842+
initializeFancyButton();
843+
else
844+
mTextView.setTypeface(mTextTypeFace, textStyle);
845+
}
846+
810847
/**
811848
* Set Custom font for button icon
812849
*

fancybuttons_library/src/main/res/values/attrs.xml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<attr name="android:textSize" />
99
<attr name="android:textAllCaps" />
1010
<attr name="android:textStyle" />
11+
<attr name="android:fontFamily" />
1112

1213
<attr name="fb_defaultColor" format="color" />
1314
<attr name="fb_text" format="string" />
@@ -16,6 +17,7 @@
1617
<attr name="fb_iconColor" format="color" />
1718

1819
<attr name="fb_textFont" format="string" />
20+
<attr name="fb_textFontRes" format="reference" />
1921
<attr name="fb_iconFont" format="string" />
2022

2123
<attr name="fb_textSize" format="dimension" />

gradle.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
VERSION_CODE=25
2-
ANDROID_BUILD_MIN_SDK_VERSION=8
2+
ANDROID_BUILD_MIN_SDK_VERSION=14
33
ANDROID_BUILD_TARGET_SDK_VERSION=27
44
ANDROID_BUILD_TOOLS_VERSION=27.0.3
55
ANDROID_BUILD_SDK_VERSION=27
6-
VERSION_NAME=1.8.4
6+
VERSION_NAME=1.9.0
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Sun Feb 11 09:10:35 WIB 2018
1+
#Sat Aug 04 21:48:19 CEST 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

samples/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ android {
1919
}
2020

2121
dependencies {
22-
compile 'com.android.support:appcompat-v7:22.2.1'
23-
compile project(':fancybuttons_library')
22+
implementation "com.android.support:appcompat-v7:$support_lib_version"
23+
implementation project(':fancybuttons_library')
2424

25-
compile fileTree(dir: 'libs', include: ['*.jar'])
25+
implementation fileTree(dir: 'libs', include: ['*.jar'])
2626
}

samples/src/main/java/mehdi/sakout/fancybuttons/samples/MainActivity.java

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,12 @@
22

33
import android.app.ListActivity;
44
import android.content.Intent;
5-
import android.graphics.Color;
6-
import android.support.v7.app.ActionBarActivity;
75
import android.os.Bundle;
8-
import android.text.Html;
96
import android.view.Menu;
107
import android.view.MenuItem;
118
import android.view.View;
12-
import android.view.ViewGroup;
139
import android.widget.AdapterView;
1410
import android.widget.ArrayAdapter;
15-
import android.widget.LinearLayout;
16-
17-
import mehdi.sakout.fancybuttons.FancyButton;
1811

1912
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {
2013

@@ -34,7 +27,7 @@ protected void onCreate(Bundle savedInstanceState) {
3427

3528
@Override
3629
public boolean onCreateOptionsMenu(Menu menu) {
37-
30+
3831
// Inflate the menu; this adds items to the action bar if it is present.
3932
getMenuInflater().inflate(R.menu.main, menu);
4033
return true;
@@ -54,17 +47,18 @@ public boolean onOptionsItemSelected(MenuItem item) {
5447

5548
@Override
5649
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
57-
switch(position){
58-
case 0 :
59-
Intent intentXML = new Intent(MainActivity.this,XmlButtons.class);
50+
switch (position) {
51+
case 0:
52+
Intent intentXML = new Intent(MainActivity.this, XmlButtons.class);
6053
startActivity(intentXML);
6154

6255
break;
63-
case 1 :
64-
Intent intentProg = new Intent(MainActivity.this,ProgramButtons.class);
56+
case 1:
57+
Intent intentProg = new Intent(MainActivity.this, ProgramButtons.class);
6558
startActivity(intentProg);
6659
break;
67-
default: throw new IllegalArgumentException("Hold up, hold my phone :)");
60+
default:
61+
throw new IllegalArgumentException("Hold up, hold my phone :)");
6862
}
6963
}
7064
}

samples/src/main/java/mehdi/sakout/fancybuttons/samples/ProgramButtons.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package mehdi.sakout.fancybuttons.samples;
22

33
import android.graphics.Color;
4-
import android.support.v7.app.ActionBar;
5-
import android.support.v7.app.ActionBarActivity;
64
import android.os.Bundle;
75
import android.support.v7.app.AppCompatActivity;
86
import android.view.Menu;
@@ -47,7 +45,8 @@ protected void onCreate(Bundle savedInstanceState) {
4745
installBtn.setFocusBackgroundColor(Color.parseColor("#bfe156"));
4846
installBtn.setTextSize(17);
4947
installBtn.setRadius(5);
50-
installBtn.setIconPadding(0,30,0,0);
48+
installBtn.setCustomTextFont(R.font.roboto_slab_bold);
49+
installBtn.setIconPadding(0, 30, 0, 0);
5150
installBtn.setEnabled(false);
5251

5352
FancyButton signupBtn = new FancyButton(this);
@@ -58,15 +57,15 @@ protected void onCreate(Bundle savedInstanceState) {
5857
signupBtn.setFocusBackgroundColor(Color.parseColor("#ffa43c"));
5958
signupBtn.setTextSize(20);
6059
signupBtn.setCustomTextFont("robotothin.ttf");
61-
signupBtn.setIconPadding(10,0,10,0);
60+
signupBtn.setIconPadding(10, 0, 10, 0);
6261

6362
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
64-
layoutParams.setMargins(0,0,0,10);
63+
layoutParams.setMargins(0, 0, 0, 10);
6564

66-
LinearLayout container = (LinearLayout)findViewById(R.id.container);
67-
container.addView(facebookLoginBtn,layoutParams);
68-
container.addView(foursquareBtn,layoutParams);
69-
container.addView(installBtn,layoutParams);
65+
LinearLayout container = (LinearLayout) findViewById(R.id.container);
66+
container.addView(facebookLoginBtn, layoutParams);
67+
container.addView(foursquareBtn, layoutParams);
68+
container.addView(installBtn, layoutParams);
7069
container.addView(signupBtn);
7170

7271
}
167 KB
Binary file not shown.

samples/src/main/res/layout/activity_xml_buttons.xml

+52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<ScrollView
22
xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:fancy="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
56
android:layout_height="wrap_content">
67

@@ -587,6 +588,57 @@
587588

588589

589590
</LinearLayout>
591+
<LinearLayout
592+
android:layout_width="match_parent"
593+
android:layout_height="wrap_content"
594+
android:layout_marginBottom="15dp"
595+
android:gravity="center"
596+
android:orientation="horizontal">
597+
598+
599+
<mehdi.sakout.fancybuttons.FancyButton
600+
android:id="@+id/btn_custom_font_resource"
601+
android:layout_width="wrap_content"
602+
android:layout_height="wrap_content"
603+
android:layout_marginRight="20dp"
604+
android:gravity="right"
605+
android:padding="15dp"
606+
android:paddingLeft="20dp"
607+
android:paddingRight="20dp"
608+
android:fontFamily="@font/roboto_slab_bold"
609+
fancy:fb_borderColor="#ffe7e6e2"
610+
fancy:fb_borderWidth="2dp"
611+
fancy:fb_focusColor="#fffffefa"
612+
fancy:fb_fontIconResource="@string/icon_creditcard"
613+
fancy:fb_fontIconSize="15sp"
614+
fancy:fb_ghost="true"
615+
fancy:fb_iconPosition="right"
616+
fancy:fb_radius="40dp"
617+
fancy:fb_text="Deposit"
618+
fancy:fb_textColor="#ffffff"
619+
tools:targetApi="jelly_bean" />
620+
<mehdi.sakout.fancybuttons.FancyButton
621+
android:id="@+id/btn_custom_font_resource_custom_attr"
622+
android:layout_width="wrap_content"
623+
android:layout_height="wrap_content"
624+
android:layout_marginRight="20dp"
625+
android:gravity="right"
626+
android:padding="15dp"
627+
android:paddingLeft="20dp"
628+
android:paddingRight="20dp"
629+
fancy:fb_textFontRes="@font/roboto_slab_bold"
630+
fancy:fb_borderColor="#ffe7e6e2"
631+
fancy:fb_borderWidth="2dp"
632+
fancy:fb_focusColor="#fffffefa"
633+
fancy:fb_fontIconResource="@string/icon_creditcard"
634+
fancy:fb_fontIconSize="15sp"
635+
fancy:fb_ghost="true"
636+
fancy:fb_iconPosition="right"
637+
fancy:fb_radius="40dp"
638+
fancy:fb_text="Deposit"
639+
fancy:fb_textColor="#ffffff"
640+
tools:targetApi="jelly_bean" />
641+
</LinearLayout>
590642

591643
</LinearLayout>
592644

0 commit comments

Comments
 (0)