Skip to content

Commit 12ecd17

Browse files
committed
Add image and text weight property.
1 parent 979cee3 commit 12ecd17

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@
7878
app:cb_elevation_enabled="true"
7979
app:cb_image="@drawable/ic_owl"
8080
app:cb_image_position="left"
81+
app:cb_image_weight="1"
8182
app:cb_shape_radius="5dp"
8283
app:cb_shape_type="rect"
8384
app:cb_text_color="@color/orange_normal"
8485
app:cb_text_color_disabled="@color/orange_light"
8586
app:cb_text_color_pressed="@color/orange_dark"
86-
app:cb_text_padding="5dp" />
87+
app:cb_text_padding="5dp"
88+
app:cb_text_weight="1" />
8789

8890
</RelativeLayout>

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
5353
private int imagePadding;
5454
private int[] imagePaddingArray;
5555
private int imageScaleTypeAttr;
56+
private int imageWeight;
57+
private int textWeight;
5658

5759
private int shapeType; // converted shape type from shapeTypeAttr
5860
private ImageView.ScaleType imageScaleType;
@@ -83,6 +85,7 @@ public CustomButton(Context context, AttributeSet attrs) {
8385
textPaddingArray[TOP] = (int) attributes.getDimension(R.styleable.CustomButton_cb_text_padding_top, textPadding);
8486
textPaddingArray[RIGHT] = (int) attributes.getDimension(R.styleable.CustomButton_cb_text_padding_right, textPadding);
8587
textPaddingArray[BOTTOM] = (int) attributes.getDimension(R.styleable.CustomButton_cb_text_padding_bottom, textPadding);
88+
textWeight = attributes.getInteger(R.styleable.CustomButton_cb_text_weight, 1);
8689

8790
shapeRadius = attributes.getDimension(R.styleable.CustomButton_cb_shape_radius, 0);
8891
shapeTypeAttr = attributes.getInt(R.styleable.CustomButton_cb_shape_type, 0);
@@ -99,12 +102,13 @@ public CustomButton(Context context, AttributeSet attrs) {
99102
drawableNormal = attributes.getDrawable(R.styleable.CustomButton_cb_image_normal);
100103
drawablePressed = attributes.getDrawable(R.styleable.CustomButton_cb_image_pressed);
101104
drawableDisabled = attributes.getDrawable(R.styleable.CustomButton_cb_image_disabled);
102-
imageScaleTypeAttr = attributes.getInteger(R.styleable.CustomButton_cb_image_scale_type, 0);
105+
imageScaleTypeAttr = attributes.getInteger(R.styleable.CustomButton_cb_image_scale_type, 3);
103106
imagePadding = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding, 0);
104107
imagePaddingArray[LEFT] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_left, imagePadding);
105108
imagePaddingArray[TOP] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_top, imagePadding);
106109
imagePaddingArray[RIGHT] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_right, imagePadding);
107110
imagePaddingArray[BOTTOM] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_bottom, imagePadding);
111+
imageWeight = attributes.getInteger(R.styleable.CustomButton_cb_image_weight, 1);
108112

109113
if (backgroundColorState != null) { // if backgroundColor state is not null the set color from color state list to specific colors
110114
backgroundColorStateListToIntegers(backgroundColorState);
@@ -166,17 +170,27 @@ public void setEnabled(boolean enabled) {
166170

167171
private void setContent(Context context) {
168172
removeAllViews();
173+
textView = null;
174+
imageContainer = null;
169175

170176
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
171-
textView = new TextView(context);
172-
setTextPadding(textPaddingArray);
173177
container = new LinearLayout(context);
174178
container.setLayoutParams(layoutParams);
175179
container.setOrientation(VERTICAL);
176-
layoutParams.weight = 1;
180+
181+
textView = new TextView(context);
182+
imageContainer = new ImageView(context);
183+
setTextPadding(textPaddingArray);
184+
185+
LayoutParams layoutParamsText = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
186+
LayoutParams layoutParamsImage = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
187+
layoutParamsText.weight = textWeight;
188+
layoutParamsImage.weight = imageWeight;
189+
190+
textView.setLayoutParams(layoutParamsText);
191+
imageContainer.setLayoutParams(layoutParamsImage);
177192

178193
if (drawablePosition < 0 && drawablePosition > 4) {
179-
textView.setLayoutParams(layoutParams);
180194
textView.setGravity(Gravity.CENTER);
181195
container.addView(textView);
182196
} else {
@@ -185,10 +199,8 @@ private void setContent(Context context) {
185199
listDrawable.addState(new int[]{android.R.attr.state_enabled}, drawableNormal);
186200
listDrawable.addState(new int[]{}, drawableDisabled);
187201

188-
imageContainer = new ImageView(context);
189202
imageContainer.setPadding(imagePaddingArray[LEFT], imagePaddingArray[TOP], imagePaddingArray[RIGHT], imagePaddingArray[BOTTOM]);
190203
imageContainer.setScaleType(imageScaleType);
191-
imageContainer.setLayoutParams(layoutParams);
192204
if (drawable != null) {
193205
imageContainer.setImageDrawable(drawable);
194206
} else {
@@ -205,7 +217,6 @@ private void setContent(Context context) {
205217
break;
206218
}
207219
if (text != null) {
208-
textView.setLayoutParams(layoutParams);
209220
textView.setGravity(Gravity.CENTER);
210221
container.addView(textView);
211222
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<attr name="cb_text_padding_top" format="dimension" />
1919
<attr name="cb_text_padding_right" format="dimension" />
2020
<attr name="cb_text_padding_bottom" format="dimension" />
21+
<attr name="cb_text_weight" format="integer" />
2122

2223
<attr name="cb_shape_radius" format="dimension" />
2324
<attr name="cb_shape_type" format="integer">
@@ -56,6 +57,7 @@
5657
<enum name="fit_end" value="5" />
5758
<enum name="fit_xy" value="6" />
5859
</attr>
60+
<attr name="cb_image_weight" format="integer" />
5961

6062
</declare-styleable>
6163
</resources>

0 commit comments

Comments
 (0)