Skip to content

Commit

Permalink
Merge pull request #118 from vkrissz/feature-add-font-resource-support
Browse files Browse the repository at this point in the history
Add support for font resources.
  • Loading branch information
medyo authored Aug 5, 2018
2 parents 495b867 + 88e90fd commit 79574f1
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 36 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ Icons, Borders, Radius ... for Android buttons

### Changelog

- 1.9.0
- Add support for font resources (For text font only)
- Increase min API level to 14
- Now you can use these to set text font (res/font/roboto_slab_bold.ttf):
android:fontFamily="@font/roboto_slab_bold"
fancy:fb_textFontRes="@font/roboto_slab_bold"
- Add dependency on support library used only when using font resources.

- 1.8.4
- Fix Text Gravity
- Add Ability to define custom radius value for each corner
Expand Down Expand Up @@ -55,7 +63,11 @@ Icons, Borders, Radius ... for Android buttons

### Installation

compile 'com.github.medyo:fancybuttons:1.8.4'
implementation 'com.github.medyo:fancybuttons:1.9.0'

### To use font resources add support library to your dependencies:

implementation "com.android.support:appcompat-v7:$support_lib_version"

### Usage

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

#### Supported Getters
| Function | Description |
Expand Down Expand Up @@ -174,6 +188,7 @@ Fancybuttons is delivered with :

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

## Contributions
Fancybuttons needs you to build the missing features :
Expand Down
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.support_lib_version = '27.1.1'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.2.0-beta05'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
}
Expand All @@ -14,5 +16,6 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}
4 changes: 3 additions & 1 deletion fancybuttons_library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ android {
}

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

archivesBaseName = 'fancybuttons'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.support.annotation.FontRes;
import android.support.v4.content.res.ResourcesCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
Expand Down Expand Up @@ -360,12 +362,31 @@ private void initAttributesArray(TypedArray attrsArray) {
? Utils.findFont(mContext, iconFontFamily, mDefaultIconFont)
: Utils.findFont(mContext, mDefaultIconFont, null);

mTextTypeFace = textFontFamily != null
? Utils.findFont(mContext, textFontFamily, mDefaultTextFont)
: Utils.findFont(mContext, mDefaultTextFont, null);
Typeface fontResource = getTypeface(attrsArray);
if (fontResource != null) {
mTextTypeFace = fontResource;
} else {
mTextTypeFace = textFontFamily != null
? Utils.findFont(mContext, textFontFamily, mDefaultTextFont)
: Utils.findFont(mContext, mDefaultTextFont, null);
}
}
}

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;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable getRippleDrawable(Drawable defaultDrawable, Drawable focusDrawable, Drawable disabledDrawable) {
if (!mEnabled) {
Expand Down Expand Up @@ -807,6 +828,22 @@ public void setCustomTextFont(String fontName) {
mTextView.setTypeface(mTextTypeFace, textStyle);
}

/**
* 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 Custom font for button icon
*
Expand Down
2 changes: 2 additions & 0 deletions fancybuttons_library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<attr name="android:textSize" />
<attr name="android:textAllCaps" />
<attr name="android:textStyle" />
<attr name="android:fontFamily" />

<attr name="fb_defaultColor" format="color" />
<attr name="fb_text" format="string" />
Expand All @@ -16,6 +17,7 @@
<attr name="fb_iconColor" format="color" />

<attr name="fb_textFont" format="string" />
<attr name="fb_textFontRes" format="reference" />
<attr name="fb_iconFont" format="string" />

<attr name="fb_textSize" format="dimension" />
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VERSION_CODE=25
ANDROID_BUILD_MIN_SDK_VERSION=8
ANDROID_BUILD_MIN_SDK_VERSION=14
ANDROID_BUILD_TARGET_SDK_VERSION=27
ANDROID_BUILD_TOOLS_VERSION=27.0.3
ANDROID_BUILD_SDK_VERSION=27
VERSION_NAME=1.8.4
VERSION_NAME=1.9.0
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Feb 11 09:10:35 WIB 2018
#Sat Aug 04 21:48:19 CEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
6 changes: 3 additions & 3 deletions samples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ android {
}

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

compile fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

import mehdi.sakout.fancybuttons.FancyButton;

public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
Expand All @@ -54,17 +47,18 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch(position){
case 0 :
Intent intentXML = new Intent(MainActivity.this,XmlButtons.class);
switch (position) {
case 0:
Intent intentXML = new Intent(MainActivity.this, XmlButtons.class);
startActivity(intentXML);

break;
case 1 :
Intent intentProg = new Intent(MainActivity.this,ProgramButtons.class);
case 1:
Intent intentProg = new Intent(MainActivity.this, ProgramButtons.class);
startActivity(intentProg);
break;
default: throw new IllegalArgumentException("Hold up, hold my phone :)");
default:
throw new IllegalArgumentException("Hold up, hold my phone :)");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package mehdi.sakout.fancybuttons.samples;

import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
Expand Down Expand Up @@ -47,7 +45,8 @@ protected void onCreate(Bundle savedInstanceState) {
installBtn.setFocusBackgroundColor(Color.parseColor("#bfe156"));
installBtn.setTextSize(17);
installBtn.setRadius(5);
installBtn.setIconPadding(0,30,0,0);
installBtn.setCustomTextFont(R.font.roboto_slab_bold);
installBtn.setIconPadding(0, 30, 0, 0);
installBtn.setEnabled(false);

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

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0,0,0,10);
layoutParams.setMargins(0, 0, 0, 10);

LinearLayout container = (LinearLayout)findViewById(R.id.container);
container.addView(facebookLoginBtn,layoutParams);
container.addView(foursquareBtn,layoutParams);
container.addView(installBtn,layoutParams);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(facebookLoginBtn, layoutParams);
container.addView(foursquareBtn, layoutParams);
container.addView(installBtn, layoutParams);
container.addView(signupBtn);

}
Expand Down
Binary file added samples/src/main/res/font/roboto_slab_bold.ttf
Binary file not shown.
52 changes: 52 additions & 0 deletions samples/src/main/res/layout/activity_xml_buttons.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fancy="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

Expand Down Expand Up @@ -587,6 +588,57 @@


</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center"
android:orientation="horizontal">


<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/btn_custom_font_resource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:gravity="right"
android:padding="15dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:fontFamily="@font/roboto_slab_bold"
fancy:fb_borderColor="#ffe7e6e2"
fancy:fb_borderWidth="2dp"
fancy:fb_focusColor="#fffffefa"
fancy:fb_fontIconResource="@string/icon_creditcard"
fancy:fb_fontIconSize="15sp"
fancy:fb_ghost="true"
fancy:fb_iconPosition="right"
fancy:fb_radius="40dp"
fancy:fb_text="Deposit"
fancy:fb_textColor="#ffffff"
tools:targetApi="jelly_bean" />
<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/btn_custom_font_resource_custom_attr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:gravity="right"
android:padding="15dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
fancy:fb_textFontRes="@font/roboto_slab_bold"
fancy:fb_borderColor="#ffe7e6e2"
fancy:fb_borderWidth="2dp"
fancy:fb_focusColor="#fffffefa"
fancy:fb_fontIconResource="@string/icon_creditcard"
fancy:fb_fontIconSize="15sp"
fancy:fb_ghost="true"
fancy:fb_iconPosition="right"
fancy:fb_radius="40dp"
fancy:fb_text="Deposit"
fancy:fb_textColor="#ffffff"
tools:targetApi="jelly_bean" />
</LinearLayout>

</LinearLayout>

Expand Down

0 comments on commit 79574f1

Please sign in to comment.