Skip to content

Commit b5a9cc5

Browse files
committed
Add simple color spimplification for each button state
1 parent 27abf87 commit b5a9cc5

File tree

5 files changed

+127
-52
lines changed

5 files changed

+127
-52
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ This is the base tag of CustomButton:<br>
1818
android:text="text"/>
1919
```
2020
<p> You can specify the following things:<br>
21-
cb_background - background color of normal button {format - color} - recommended <br>
21+
cb_primary_color - \/
22+
cb_secondary_color - you can specify only two colors for all button. But it will be override by background, text, frame color. {format - color} <br>
23+
cb_background - background color of normal button {format - color} <br>
2224
cb_background_pressed - background color of pressed button {format - color} <br>
2325
cb_background_disabled - background color of disabled button {format - color} <br>
2426
cb_background_state_list - {format - ColorStateList} <br>
@@ -58,7 +60,10 @@ cb_image_scale_type - {center, center_inside, center_crop, fit_center, fit_start
5860
cb_image_weight - {format - integer} </p>
5961
<h3> Java Code </h3>
6062
```java
61-
CustomButton(Context context, LayoutParams layoutParams, int backgroundColor, int textColor, Drawable icon)
63+
CustomButton(Context context, LayoutParams layoutParams, int primaryColor, int secondaryColor, Drawable icon)
64+
setPrimaryColor(int color)
65+
setSecondaryColor(int color)
66+
setMainColors(int primaryColor, int secondaryColor)
6267
setShapeBackground(int shapeType, int shapeRadius)
6368
setBackgroundColorStateList(ColorStateList colorStateList)
6469
setBackgroundColor(int color)

app/src/main/java/pl/sigmapoint/StartFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private void init() {
9999
array[CustomButton.RIGHT] = "Right";
100100
array[CustomButton.BOTTOM] = "Bottom";
101101

102-
ArrayAdapter<String> adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, array);
102+
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, array);
103103
iconPositionS.setAdapter(adapter);
104104

105105
colorPickerDialog = new AmbilWarnaDialog(getActivity(), Color.WHITE, new AmbilWarnaDialog.OnAmbilWarnaListener() {

app/src/main/res/layout/fragment_start.xml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,9 @@
312312
android:layout_weight="1"
313313
android:padding="2dp"
314314
android:text="@string/generate"
315-
app:cb_background="@color/blue_normal"
316-
app:cb_background_disabled="@color/blue_light"
317-
app:cb_background_pressed="@color/blue_dark"
318-
app:cb_elevation_enabled="true"
319-
app:cb_frame_color="@color/red_normal"
320-
app:cb_frame_color_disabled="@color/red_light"
321-
app:cb_frame_color_pressed="@color/red_dark"
322315
app:cb_frame_size="5dp"
323-
app:cb_shape_radius="5dp"
324-
app:cb_shape_type="rect"
325-
app:cb_text_color="@color/pink_normal"
326-
app:cb_text_color_disabled="@color/pink_light"
327-
app:cb_text_color_pressed="@color/pink_dark"
328-
app:cb_text_padding="5dp"
329-
app:cb_text_size="30dp" />
316+
app:cb_primary_color="@color/blue_normal"
317+
app:cb_secondary_color="@color/red_normal"
318+
app:cb_shape_radius="5dp" />
330319

331320
</LinearLayout>

library/src/main/java/pl/sigmapoint/customview/CustomButton.java

Lines changed: 113 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.Context;
55
import android.content.res.ColorStateList;
66
import android.content.res.TypedArray;
7+
import android.graphics.Color;
78
import android.graphics.drawable.Drawable;
89
import android.graphics.drawable.GradientDrawable;
910
import android.graphics.drawable.RippleDrawable;
@@ -38,6 +39,7 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
3839
protected LinearLayout container; //all content container
3940
protected ImageView imageContainer;
4041

42+
private int primaryColor, secondaryColor;
4143
private int backgroundColorPressed, backgroundColorDisabled, backgroundColorNormal; // colors for each backgroundColorNormal state
4244
private ColorStateList backgroundColorState; // color state list for backgroundColorNormal
4345
private int textColorPressed, textColorDisabled, textColorNormal; // colors for each text color state
@@ -74,29 +76,36 @@ private void init(Context context) {
7476
imageContainer = new ImageView(context);
7577
}
7678

77-
public CustomButton(Context context, ViewGroup.LayoutParams params, int backgroundColorNormal, int textColorNormal, Drawable imageNormal) {
79+
public CustomButton(Context context, ViewGroup.LayoutParams params, int primaryColor, int secondaryColor, Drawable imageNormal) {
7880
super(context);
7981
init(context);
8082
setLayoutParams(params);
8183

82-
this.backgroundColorNormal = backgroundColorNormal;
83-
this.textColorNormal = textColorNormal;
84+
this.primaryColor = primaryColor;
85+
this.secondaryColor = secondaryColor;
86+
8487
this.drawableNormal = imageNormal;
8588

86-
this.backgroundColorPressed = backgroundColorNormal;
87-
this.backgroundColorDisabled = backgroundColorNormal;
89+
this.backgroundColorNormal = primaryColor;
90+
this.backgroundColorPressed = secondaryColor;
91+
this.backgroundColorDisabled = secondaryColor;
8892

89-
this.textColorDisabled = textColorNormal;
90-
this.textColorPressed = textColorNormal;
93+
this.textColorNormal = secondaryColor;
94+
this.textColorDisabled = primaryColor;
95+
this.textColorPressed = primaryColor;
9196

92-
shapeType = 0;
93-
shapeRadius = 0;
94-
imageScaleType = ImageView.ScaleType.FIT_CENTER;
97+
this.frameColorNormal = secondaryColor;
98+
this.frameColorPressed = primaryColor;
99+
this.frameColorDisabled = primaryColor;
95100

96-
if (drawableNormal != null) drawablePosition = LEFT; //default position
101+
this.shapeType = 0;
102+
this.shapeRadius = 0;
103+
this.imageScaleType = ImageView.ScaleType.FIT_CENTER;
104+
105+
if (drawableNormal != null) this.drawablePosition = LEFT; //default position
97106
else drawablePosition = -1;
98107

99-
setContent(context);
108+
setContent();
100109

101110
setShapeBackground(); // set shape and backgroundColorNormal to button
102111
setElevationEnabled(true); // this method will work only for post-L android. Set elevation or disable it and set margins if needed
@@ -112,14 +121,18 @@ public CustomButton(Context context, AttributeSet attrs) {
112121
TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomButton, 0, 0);
113122

114123
try {
115-
backgroundColorNormal = attributes.getColor(R.styleable.CustomButton_cb_background, 0);
116-
backgroundColorPressed = attributes.getColor(R.styleable.CustomButton_cb_background_pressed, backgroundColorNormal);
117-
backgroundColorDisabled = attributes.getColor(R.styleable.CustomButton_cb_background_disabled, backgroundColorNormal);
124+
125+
primaryColor = attributes.getColor(R.styleable.CustomButton_cb_primary_color, Color.LTGRAY);
126+
secondaryColor = attributes.getColor(R.styleable.CustomButton_cb_secondary_color, Color.GRAY);
127+
128+
backgroundColorNormal = attributes.getColor(R.styleable.CustomButton_cb_background, primaryColor);
129+
backgroundColorPressed = attributes.getColor(R.styleable.CustomButton_cb_background_pressed, secondaryColor);
130+
backgroundColorDisabled = attributes.getColor(R.styleable.CustomButton_cb_background_disabled, secondaryColor);
118131
backgroundColorState = attributes.getColorStateList(R.styleable.CustomButton_cb_background_state_list);
119132

120-
textColorNormal = attributes.getColor(R.styleable.CustomButton_cb_text_color, 0);
121-
textColorPressed = attributes.getColor(R.styleable.CustomButton_cb_text_color_pressed, textColorNormal);
122-
textColorDisabled = attributes.getColor(R.styleable.CustomButton_cb_text_color_disabled, textColorNormal);
133+
textColorNormal = attributes.getColor(R.styleable.CustomButton_cb_text_color, secondaryColor);
134+
textColorPressed = attributes.getColor(R.styleable.CustomButton_cb_text_color_pressed, primaryColor);
135+
textColorDisabled = attributes.getColor(R.styleable.CustomButton_cb_text_color_disabled, primaryColor);
123136
textColorState = attributes.getColorStateList(R.styleable.CustomButton_android_textColor);
124137
textSize = attributes.getDimension(R.styleable.CustomButton_cb_text_size, 0);
125138
text = attributes.getString(R.styleable.CustomButton_android_text);
@@ -132,9 +145,9 @@ public CustomButton(Context context, AttributeSet attrs) {
132145

133146
shapeRadius = attributes.getDimension(R.styleable.CustomButton_cb_shape_radius, 0);
134147
shapeTypeAttr = attributes.getInt(R.styleable.CustomButton_cb_shape_type, 0);
135-
frameColorNormal = attributes.getColor(R.styleable.CustomButton_cb_frame_color, 0);
136-
frameColorPressed = attributes.getColor(R.styleable.CustomButton_cb_frame_color_pressed, frameColorNormal);
137-
frameColorDisabled = attributes.getColor(R.styleable.CustomButton_cb_frame_color_disabled, frameColorNormal);
148+
frameColorNormal = attributes.getColor(R.styleable.CustomButton_cb_frame_color, secondaryColor);
149+
frameColorPressed = attributes.getColor(R.styleable.CustomButton_cb_frame_color_pressed, primaryColor);
150+
frameColorDisabled = attributes.getColor(R.styleable.CustomButton_cb_frame_color_disabled, primaryColor);
138151
frameColorState = attributes.getColorStateList(R.styleable.CustomButton_cb_frame_state_list);
139152
frameSize = attributes.getDimension(R.styleable.CustomButton_cb_frame_size, 0);
140153

@@ -187,7 +200,7 @@ public CustomButton(Context context, AttributeSet attrs) {
187200
break;
188201
}
189202

190-
setContent(context);
203+
setContent();
191204

192205
shapeType = (shapeTypeAttr == 0) ? GradientDrawable.RECTANGLE : GradientDrawable.OVAL; // convert xml attributes value to shape type constant
193206

@@ -213,7 +226,7 @@ public void setEnabled(boolean enabled) {
213226
container.setEnabled(enabled);
214227
}
215228

216-
private void setContent(Context context) {
229+
private void setContent() {
217230
removeAllViews();
218231
container.removeAllViews();
219232

@@ -291,15 +304,7 @@ private void setContent(Context context) {
291304

292305
addView(container);
293306

294-
textColorArray = new int[]{textColorPressed, textColorNormal, textColorDisabled};
295-
textColorList = new ColorStateList(stateArray, textColorArray);
296-
297-
if (text != null) {
298-
textView.setText(text);
299-
if (textSize > 0) textView.setTextSize(textSize);
300-
if (textColorNormal != 0) textView.setTextColor(textColorList);
301-
else if (textColorState != null) textView.setTextColor(textColorState);
302-
}
307+
updateText();
303308
}
304309

305310
/**
@@ -380,6 +385,46 @@ private void frameColorStateListToIntegers(ColorStateList colorStateList) {
380385
frameColorDisabled = globalColor;
381386
}
382387

388+
private void updateText() {
389+
textColorArray = new int[]{textColorPressed, textColorNormal, textColorDisabled};
390+
textColorList = new ColorStateList(stateArray, textColorArray);
391+
392+
if (text != null) {
393+
textView.setText(text);
394+
if (textSize > 0) textView.setTextSize(textSize);
395+
if (textColorNormal != 0) textView.setTextColor(textColorList);
396+
else if (textColorState != null) textView.setTextColor(textColorState);
397+
} else {
398+
container.removeView(textView);
399+
}
400+
}
401+
402+
private void updatePrimaryColor(int color) {
403+
404+
this.primaryColor = color;
405+
406+
this.backgroundColorNormal = color;
407+
408+
this.textColorDisabled = color;
409+
this.textColorPressed = color;
410+
411+
this.frameColorPressed = color;
412+
this.frameColorDisabled = color;
413+
}
414+
415+
private void updateSecondaryColor(int color) {
416+
417+
this.secondaryColor = color;
418+
419+
this.backgroundColorPressed = color;
420+
this.backgroundColorDisabled = color;
421+
422+
this.textColorNormal = color;
423+
424+
this.frameColorNormal = color;
425+
}
426+
427+
383428
/**
384429
* Set shape backgroundColorNormal.
385430
*
@@ -392,6 +437,32 @@ public void setShapeBackground(int shapeType, int shapeRadius) {
392437
setShapeBackground();
393438
}
394439

440+
441+
public void setPrimaryColor(int color) {
442+
443+
updatePrimaryColor(color);
444+
445+
updateText();
446+
setShapeBackground();
447+
}
448+
449+
public void setSecondaryColor(int color) {
450+
451+
updateSecondaryColor(color);
452+
453+
updateText();
454+
setShapeBackground();
455+
}
456+
457+
public void setMainColors(int primaryColor, int secondaryColor) {
458+
459+
updatePrimaryColor(primaryColor);
460+
updateSecondaryColor(secondaryColor);
461+
462+
updateText();
463+
setShapeBackground();
464+
}
465+
395466
/**
396467
* Set background color from color state list.
397468
* Only three states are use: disabled, pressed, normal.
@@ -507,7 +578,7 @@ public void setText(String text) {
507578
this.text = text;
508579
textView.setText(text);
509580
if (textView.getParent() != container) {
510-
setContent(getContext());
581+
setContent();
511582
}
512583
}
513584

@@ -605,7 +676,7 @@ public void setImage(int position, Drawable drawableNormal, Drawable drawablePre
605676
this.imageWeight = weight;
606677
this.imagePaddingArray = padding;
607678

608-
setContent(getContext());
679+
setContent();
609680
}
610681

611682
/**
@@ -629,7 +700,7 @@ public void setImage(int position, Drawable drawable, ImageView.ScaleType scaleT
629700
if (scaleType != null)
630701
imageContainer.setScaleType(scaleType);
631702

632-
setContent(getContext());
703+
setContent();
633704
}
634705

635706
/**
@@ -797,4 +868,11 @@ public int getTextWeight() {
797868
return textWeight;
798869
}
799870

871+
public int getPrimaryColor() {
872+
return primaryColor;
873+
}
874+
875+
public int getSecondaryColor() {
876+
return secondaryColor;
877+
}
800878
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<resources>
33
<declare-styleable name="CustomButton">
44

5+
<attr name="cb_primary_color" format="color" />
6+
<attr name="cb_secondary_color" format="color" />
7+
58
<attr name="cb_background_pressed" format="color" />
69
<attr name="cb_background_disabled" format="color" />
710
<attr name="cb_background" format="color" />

0 commit comments

Comments
 (0)