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

added code to be able to change label text color and background color #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class FloatingActionButton extends ImageButton {
int mColorPressed;
int mColorDisabled;
String mTitle;
int mTitleColor;
int mTitleBackgroundColor;
@DrawableRes
private int mIcon;
private Drawable mIconDrawable;
Expand Down Expand Up @@ -79,6 +81,8 @@ void init(Context context, AttributeSet attributeSet) {
mSize = attr.getInt(R.styleable.FloatingActionButton_fab_size, SIZE_NORMAL);
mIcon = attr.getResourceId(R.styleable.FloatingActionButton_fab_icon, 0);
mTitle = attr.getString(R.styleable.FloatingActionButton_fab_title);
mTitleColor = attr.getColor(R.styleable.FloatingActionButton_fab_titleColor, 0);
mTitleBackgroundColor = attr.getColor(R.styleable.FloatingActionButton_fab_titleBackgroundColor, 0);
mStrokeVisible = attr.getBoolean(R.styleable.FloatingActionButton_fab_stroke_visible, true);
attr.recycle();

Expand Down Expand Up @@ -213,6 +217,22 @@ public void setTitle(String title) {
}
}

public void setTitleColor(int color) {
mTitleColor = color;
TextView label = getLabelView();
if (label != null) {
label.setTextColor(color);
}
}

public void setTitleBackgroundColor(int color) {
mTitleBackgroundColor = color;
TextView label = getLabelView();
if (label != null) {
label.setBackgroundColor(color);
}
}

TextView getLabelView() {
return (TextView) getTag(R.id.fab_label);
}
Expand All @@ -221,6 +241,14 @@ public String getTitle() {
return mTitle;
}

public int getTitleColor() {
return mTitleColor;
}

public int getTitleBackgroundColor() {
return mTitleBackgroundColor;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Expand Down Expand Up @@ -426,4 +454,4 @@ public void setVisibility(int visibility) {

super.setVisibility(visibility);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ private void createLabels() {
TextView label = new TextView(context);
label.setTextAppearance(getContext(), mLabelsStyle);
label.setText(button.getTitle());

if (button.getTitleColor() != 0)
label.setTextColor(button.getTitleColor());
if (button.getTitleBackgroundColor() != 0)
label.setBackgroundColor(button.getTitleBackgroundColor());
addView(label);

button.setTag(R.id.fab_label, label);
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<enum name="mini" value="1"/>
</attr>
<attr name="fab_title" format="string"/>
<attr name="fab_titleColor" format="color"/>
<attr name="fab_titleBackgroundColor" format="color"/>
<attr name="fab_stroke_visible" format="boolean"/>
</declare-styleable>
<declare-styleable name="AddFloatingActionButton">
Expand Down