Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add text color attribute #23

Merged
merged 2 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,25 @@ The recommended way to customize the background color is by using the `app:backg
android:id="@+id/counter_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#009688"
app:backgroundTint="@color/colorAccent"
android:src="@drawable/ic_add_white_24dp" />
```

To change the badge background color you can use the `app:badgeBackgroundColor` attribute
To change the badge style you can use these attributes:

```xml
<com.andremion.counterfab.CounterFab
android:id="@+id/counter_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:badgeBackgroundColor="#009688"
android:src="@drawable/ic_add_white_24dp" />
```
- `app:badgeBackgroundColor`
- `app:badgeTextColor`
- `app:badgePosition` as `RightTop`, `LeftBottom`, `LeftTop` or `RightBottom`

To change the badge position you can use the `app:badgePosition` attribute as: RightTop, LeftBottom, LeftTop and RightBottom.
For example:

```xml
<com.andremion.counterfab.CounterFab
android:id="@+id/counter_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:badgeBackgroundColor="@color/red"
app:badgeTextColor="@color/white"
app:badgePosition="RightTop"
android:src="@drawable/ic_add_white_24dp" />
```
Expand Down
66 changes: 37 additions & 29 deletions counterfab/src/main/java/com/andremion/counterfab/CounterFab.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
Expand All @@ -40,6 +41,8 @@
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;

import static android.support.design.widget.FloatingActionButton.*;

/**
* A {@link FloatingActionButton} subclass that shows a counter badge on right top corner.
*/
Expand Down Expand Up @@ -82,7 +85,7 @@ public Float get(CounterFab object) {

private int mCount;
private String mText;
private float mTextHeight;
private final float mTextHeight;
private ObjectAnimator mAnimator;

private int badgePosition = RIGHT_TOP_POSITION;
Expand All @@ -99,18 +102,12 @@ public CounterFab(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public CounterFab(Context context, AttributeSet attrs, int defStyleAttr) {
public CounterFab(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

TypedArray ta = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.CounterFab,
0,
0);

setUseCompatPadding(true);

final float density = getResources().getDisplayMetrics().density;

float density = getResources().getDisplayMetrics().density;
mTextSize = TEXT_SIZE_DP * density;
float textPadding = TEXT_PADDING_DP * density;

Expand All @@ -119,29 +116,16 @@ public CounterFab(Context context, AttributeSet attrs, int defStyleAttr) {

mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setStyle(Paint.Style.STROKE);
mTextPaint.setColor(Color.WHITE);
mTextPaint.setTextSize(mTextSize);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mTextPaint.setTypeface(Typeface.SANS_SERIF);

mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setStyle(Paint.Style.FILL);

int defaultBadgeColor = mCirclePaint.getColor();

ColorStateList colorStateList = getBackgroundTintList();
if (colorStateList != null) {
defaultBadgeColor = colorStateList.getDefaultColor();
} else {
Drawable background = getBackground();
if (background instanceof ColorDrawable) {
ColorDrawable colorDrawable = (ColorDrawable) background;
defaultBadgeColor = colorDrawable.getColor();
}
}
int defaultBadgeColor = getDefaultBadgeColor();

mCirclePaint.setColor(ta.getColor(R.styleable.CounterFab_badgeBackgroundColor, defaultBadgeColor));
badgePosition = ta.getInt(R.styleable.CounterFab_badgePosition, RIGHT_TOP_POSITION);
setupFromStyledAttributes(context, attrs, defaultBadgeColor);

mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mMaskPaint.setStyle(Paint.Style.FILL);
Expand All @@ -166,8 +150,32 @@ public CounterFab(Context context, AttributeSet attrs, int defStyleAttr) {
onCountChanged();
}

private int getDefaultBadgeColor() {
int defaultBadgeColor = mCirclePaint.getColor();
ColorStateList colorStateList = getBackgroundTintList();
if (colorStateList != null) {
defaultBadgeColor = colorStateList.getDefaultColor();
} else {
Drawable background = getBackground();
if (background instanceof ColorDrawable) {
ColorDrawable colorDrawable = (ColorDrawable) background;
defaultBadgeColor = colorDrawable.getColor();
}
}
return defaultBadgeColor;
}

private void setupFromStyledAttributes(Context context, @Nullable AttributeSet attrs, int defaultBadgeColor) {
TypedArray styledAttributes = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.CounterFab, 0, 0);
mTextPaint.setColor(styledAttributes.getColor(R.styleable.CounterFab_badgeTextColor, Color.WHITE));
mCirclePaint.setColor(styledAttributes.getColor(R.styleable.CounterFab_badgeBackgroundColor, defaultBadgeColor));
badgePosition = styledAttributes.getInt(R.styleable.CounterFab_badgePosition, RIGHT_TOP_POSITION);
styledAttributes.recycle();
}

private boolean isSizeMini() {
return super.getSize() == android.support.design.widget.FloatingActionButton.SIZE_MINI;
return getSize() == SIZE_MINI;
}

/**
Expand Down Expand Up @@ -281,14 +289,14 @@ protected void onDraw(Canvas canvas) {
}
}

private static class SavedState extends View.BaseSavedState {
private static final class SavedState extends View.BaseSavedState {

private int count;

/**
* Constructor called from {@link CounterFab#onSaveInstanceState()}
*/
private SavedState(Parcelable superState) {
private SavedState(@Nullable Parcelable superState) {
super(superState);
}

Expand Down Expand Up @@ -321,9 +329,9 @@ public void writeToParcel(Parcel out, int flags) {

@Override
public String toString() {
return CounterFab.class.getSimpleName() + "." + SavedState.class.getSimpleName() + "{"
return CounterFab.class.getSimpleName() + '.' + SavedState.class.getSimpleName() + '{'
+ Integer.toHexString(System.identityHashCode(this))
+ " count=" + count + "}";
+ " count=" + count + '}';
}

public static final Creator<SavedState> CREATOR = new ClassLoaderCreator<SavedState>() {
Expand Down
1 change: 1 addition & 0 deletions counterfab/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CounterFab">
<attr name="badgeTextColor" format="color" />
<attr name="badgeBackgroundColor" format="color" />
<attr name="badgePosition" format="enum">
<enum name="RightTop" value="0" />
Expand Down