Skip to content

Commit d64cab0

Browse files
committed
Bug fixes and improvments in library. Update demo.
1 parent 534a1b0 commit d64cab0

File tree

10 files changed

+108
-39
lines changed

10 files changed

+108
-39
lines changed
780 Bytes
Loading
246 Bytes
Loading
501 Bytes
Loading
599 Bytes
Loading
1.58 KB
Loading
2.36 KB
Loading

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

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<android.widget.Space
1010
android:layout_width="match_parent"
1111
android:layout_height="0dp"
12-
android:layout_weight="22" />
12+
android:layout_weight="20" />
1313

1414
<pl.sigmapoint.customview.CustomButton
1515
android:layout_width="match_parent"
@@ -67,26 +67,54 @@
6767
app:cb_shape_radius="5dp"
6868
app:cb_text_color="#FFFFFF" />
6969

70-
<pl.sigmapoint.customview.CustomButton
71-
android:id="@+id/own"
70+
<LinearLayout
7271
android:layout_width="match_parent"
7372
android:layout_height="0dp"
74-
android:layout_weight="5"
73+
android:layout_weight="10"
74+
android:orientation="horizontal"
7575
android:paddingLeft="15dp"
7676
android:paddingRight="15dp"
77-
android:paddingTop="5dp"
78-
android:text="Create own button"
79-
app:cb_image_color_disabled="#c0c0c0"
80-
app:cb_image_color_normal="#FFFFFF"
81-
app:cb_image_color_pressed="#f1c8c2"
82-
app:cb_primary_color="#E33F36"
83-
app:cb_secondary_color="#5a1915"
84-
app:cb_shape_radius="5dp"
85-
app:cb_text_color="#FFFFFF" />
77+
android:paddingTop="5dp">
78+
79+
<pl.sigmapoint.customview.CustomButton
80+
android:id="@+id/own"
81+
android:layout_width="0dp"
82+
android:layout_height="match_parent"
83+
android:layout_weight="1"
84+
android:paddingRight="2dp"
85+
android:text="Create own button"
86+
app:cb_background="@android:color/transparent"
87+
app:cb_frame_color="#DBBA23"
88+
app:cb_frame_color_disabled="#CFB957"
89+
app:cb_frame_color_pressed="#A88D13"
90+
app:cb_frame_size="5dp"
91+
app:cb_primary_color="#DB433B"
92+
app:cb_secondary_color="#E01E14" />
93+
94+
95+
<pl.sigmapoint.customview.CustomButton
96+
android:layout_width="0dp"
97+
android:layout_height="match_parent"
98+
android:layout_weight="1"
99+
android:paddingLeft="2dp"
100+
android:text="Remove"
101+
app:cb_frame_size="5dp"
102+
app:cb_image="@drawable/ic_remove"
103+
app:cb_image_color_disabled="#ffffff"
104+
app:cb_image_color_normal="#e01e14"
105+
app:cb_image_color_pressed="#ffffff"
106+
app:cb_image_padding_top="6dp"
107+
app:cb_image_position="top"
108+
app:cb_primary_color="#FFFFFF"
109+
app:cb_secondary_color="#E01E14"
110+
app:cb_shape_radius="5dp"
111+
app:cb_text_padding_bottom="5dp" />
112+
113+
</LinearLayout>
86114

87115
<android.widget.Space
88116
android:layout_width="match_parent"
89117
android:layout_height="0dp"
90-
android:layout_weight="22" />
118+
android:layout_weight="20" />
91119

92120
</LinearLayout>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@
313313
android:padding="2dp"
314314
android:text="@string/generate"
315315
app:cb_frame_size="5dp"
316+
app:cb_image="@drawable/ic_owl"
317+
app:cb_image_position="left"
316318
app:cb_primary_color="@color/blue_normal"
317319
app:cb_secondary_color="@color/red_normal"
318320
app:cb_shape_radius="5dp" />

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

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
5555
private int textColorPressed, textColorDisabled, textColorNormal; // colors for each text color state
5656
private ColorStateList textColorState; // color state list for text
5757
private float textSize;
58-
private String text; // text inside button
58+
private String text;
5959
private int textPadding;
6060
private int[] textPaddingArray;
6161
private int textWeight;
6262
private float shapeRadius; // corner radius
6363
private int shapeTypeAttr; // shape type (from xml - converted to shape type constants)
6464
private int frameColorPressed, frameColorDisabled, frameColorNormal; // colors for each frame color state
6565
private ColorStateList frameColorState;
66-
private float frameSize; // frame size
66+
private float frameSize;
6767
private boolean isElevationEnabled; // is elevation should be displayed >=API 21
6868
private Drawable drawableNormal, drawableDisabled, drawablePressed;
6969
private int drawableColorNormal, drawableColorDisabled, drawableColorPressed;
@@ -86,8 +86,17 @@ private void init(Context context) {
8686
container = new LinearLayout(context);
8787
textView = new TextView(context);
8888
imageContainer = new ImageView(context);
89+
90+
setGravity(Gravity.TOP);
8991
}
9092

93+
/**
94+
* @param context application context
95+
* @param params layout parameters (width, height, weight, etc.)
96+
* @param primaryColor primary color of button
97+
* @param secondaryColor secondary color of button
98+
* @param imageNormal can be null if button doesn't have image
99+
*/
91100
public CustomButton(Context context, ViewGroup.LayoutParams params, int primaryColor, int secondaryColor, Drawable imageNormal) {
92101
super(context);
93102
init(context);
@@ -117,6 +126,12 @@ public CustomButton(Context context, ViewGroup.LayoutParams params, int primaryC
117126
setOnClickListener(this);
118127
}
119128

129+
/**
130+
* If you use it, you must specify all parameters from setters.
131+
*
132+
* @param context
133+
* @param attrs
134+
*/
120135
public CustomButton(Context context, AttributeSet attrs) {
121136
super(context, attrs);
122137

@@ -183,24 +198,27 @@ public CustomButton(Context context, AttributeSet attrs) {
183198
frameColorStateListToIntegers(frameColorState);
184199
}
185200

201+
if (backgroundColorNormal != primaryColor && backgroundColorPressed == secondaryColor && backgroundColorDisabled == lighterColor(primaryColor)) { //if only normal color is specified
202+
backgroundColorPressed = backgroundColorNormal;
203+
backgroundColorDisabled = backgroundColorNormal;
204+
}
205+
206+
if (textColorNormal != secondaryColor && textColorPressed == primaryColor && textColorDisabled == lighterColor(secondaryColor)) { //if only normal color is specified
207+
textColorPressed = textColorNormal;
208+
textColorDisabled = textColorNormal;
209+
}
210+
211+
if (drawableNormal != null) { //if only normal image is specified
212+
if (drawablePressed == null)
213+
drawablePressed = drawableNormal.getConstantState().newDrawable().mutate();
214+
if (drawableDisabled == null)
215+
drawableDisabled = drawableNormal.getConstantState().newDrawable().mutate();
216+
}
217+
186218
if (drawableColorStateListAttr != null)
187219
imageColorStateListToIntegers(drawableColorStateListAttr);
188-
setImageColorStateList();
189-
if (drawableNormal != null) {
190-
if (drawableColorNormal != Integer.MAX_VALUE) {
191-
drawableNormal = changeDrawableColor(((BitmapDrawable) drawableNormal).getBitmap(), drawableColorNormal);
192-
}
193-
if (drawableColorPressed != Integer.MAX_VALUE) {
194-
if (drawablePressed == null)
195-
drawablePressed = drawableNormal.getConstantState().newDrawable().mutate();
196-
drawablePressed = changeDrawableColor(((BitmapDrawable) drawablePressed).getBitmap(), drawableColorPressed);
197-
}
198-
if (drawableColorDisabled != Integer.MAX_VALUE) {
199-
if (drawableDisabled == null)
200-
drawableDisabled = drawableNormal.getConstantState().newDrawable().mutate();
201-
drawableDisabled = changeDrawableColor(((BitmapDrawable) drawableDisabled).getBitmap(), drawableColorDisabled);
202-
}
203-
}
220+
221+
setImageColorStateList(); //works only for images, don't work for shape, etc.
204222

205223
//WARNING: If you want to change it, you should change it in attr xml too
206224
switch (imageScaleTypeAttr) {
@@ -424,7 +442,14 @@ private void frameColorStateListToIntegers(ColorStateList colorStateList) {
424442
frameColorNormal = colorStateList.getColorForState(new int[]{android.R.attr.state_enabled}, globalColor);
425443
frameColorDisabled = globalColor;
426444
}
427-
445+
/**
446+
* Convert color state list to three integers. It is helper method to set image color.
447+
*
448+
* @param colorStateList is a frame color state list which should be converted. Should have three states:
449+
* enabled - for normal state,
450+
* pressed - for pressed state,
451+
* (empty) - for disabled state.
452+
*/
428453
private void imageColorStateListToIntegers(ColorStateList colorStateList) {
429454

430455
int globalColor = colorStateList.getColorForState(new int[]{}, 0);
@@ -434,6 +459,9 @@ private void imageColorStateListToIntegers(ColorStateList colorStateList) {
434459
drawableColorDisabled = globalColor;
435460
}
436461

462+
/**
463+
* Update text, color and size. Helper method.
464+
*/
437465
private void updateText() {
438466
textColorArray = new int[]{textColorPressed, textColorNormal, textColorDisabled};
439467
textColorList = new ColorStateList(stateArray, textColorArray);
@@ -485,17 +513,18 @@ private int lighterColor(int color) {
485513

486514
private void setImageColorStateList() {
487515
if (drawableNormal != null) {
516+
if (drawablePressed == null)
517+
drawablePressed = drawableNormal.getConstantState().newDrawable().mutate();
518+
if (drawableDisabled == null)
519+
drawableDisabled = drawableNormal.getConstantState().newDrawable().mutate();
520+
488521
if (drawableColorNormal != Integer.MAX_VALUE) {
489522
drawableNormal = changeDrawableColor(((BitmapDrawable) drawableNormal).getBitmap(), drawableColorNormal);
490523
}
491524
if (drawableColorPressed != Integer.MAX_VALUE) {
492-
if (drawablePressed == null)
493-
drawablePressed = drawableNormal.getConstantState().newDrawable().mutate();
494525
drawablePressed = changeDrawableColor(((BitmapDrawable) drawablePressed).getBitmap(), drawableColorPressed);
495526
}
496527
if (drawableColorDisabled != Integer.MAX_VALUE) {
497-
if (drawableDisabled == null)
498-
drawableDisabled = drawableNormal.getConstantState().newDrawable().mutate();
499528
drawableDisabled = changeDrawableColor(((BitmapDrawable) drawableDisabled).getBitmap(), drawableColorDisabled);
500529
}
501530
}
@@ -626,6 +655,16 @@ public void setBackgroundColorDisabled(int backgroundColorDisabled) {
626655

627656
public void setBackgroundColorNormal(int backgroundColorNormal) {
628657
this.backgroundColorNormal = backgroundColorNormal;
658+
659+
if (backgroundColorPressed == 0 && backgroundColorDisabled == 0) {
660+
backgroundColorPressed = backgroundColorNormal;
661+
backgroundColorDisabled = backgroundColorNormal;
662+
}
663+
664+
if (textColorPressed == 0 && textColorDisabled == 0) {
665+
textColorPressed = textColorNormal;
666+
textColorDisabled = textColorNormal;
667+
}
629668
setShapeBackground();
630669
}
631670

@@ -873,7 +912,7 @@ public void setElevationEnabled(boolean enabled) {
873912
int var;
874913

875914
if (enabled) {
876-
var = 6;
915+
var = 3;
877916
container.setStateListAnimator(AnimatorInflater.loadStateListAnimator(getContext(), R.anim.elevation_button_custom));
878917
} else {
879918
var = 0;

library/src/main/res/anim-v21/elevation_button_custom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<objectAnimator
1313
android:duration="75"
1414
android:propertyName="elevation"
15-
android:valueTo="3dp"
15+
android:valueTo="2dp"
1616
android:valueType="floatType" />
1717
</set>
1818
</item>

0 commit comments

Comments
 (0)