|
23 | 23 | import android.view.Gravity;
|
24 | 24 | import android.view.View;
|
25 | 25 | import android.view.ViewGroup;
|
| 26 | +import android.view.ViewTreeObserver; |
26 | 27 | import android.widget.ImageView;
|
27 | 28 | import android.widget.LinearLayout;
|
28 | 29 | import android.widget.TextView;
|
@@ -108,6 +109,7 @@ public CustomButton(Context context, ViewGroup.LayoutParams params, int primaryC
|
108 | 109 | else drawablePosition = -1;
|
109 | 110 |
|
110 | 111 | setContent();
|
| 112 | + setButtonLayoutParams(); |
111 | 113 |
|
112 | 114 | setShapeBackground(); // set shape and backgroundColorNormal to button
|
113 | 115 | setElevationEnabled(true); // this method will work only for post-L android. Set elevation or disable it and set margins if needed
|
@@ -171,7 +173,7 @@ public CustomButton(Context context, AttributeSet attrs) {
|
171 | 173 | imagePaddingArray[TOP] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_top, imagePadding);
|
172 | 174 | imagePaddingArray[RIGHT] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_right, imagePadding);
|
173 | 175 | imagePaddingArray[BOTTOM] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_bottom, imagePadding);
|
174 |
| - imageWeight = attributes.getInteger(R.styleable.CustomButton_cb_image_weight, 1); |
| 176 | + imageWeight = attributes.getInteger(R.styleable.CustomButton_cb_image_weight, 0); |
175 | 177 |
|
176 | 178 | if (backgroundColorState != null) { // if backgroundColorNormal state is not null the set color from color state list to specific colors
|
177 | 179 | backgroundColorStateListToIntegers(backgroundColorState);
|
@@ -237,6 +239,8 @@ public CustomButton(Context context, AttributeSet attrs) {
|
237 | 239 | }
|
238 | 240 |
|
239 | 241 | setOnClickListener(this);
|
| 242 | + |
| 243 | + setButtonLayoutParams(); |
240 | 244 | }
|
241 | 245 |
|
242 | 246 | @Override
|
@@ -271,11 +275,14 @@ private void setContent() {
|
271 | 275 | int width = (drawablePosition % 2 == 0) ? 0 : ViewGroup.LayoutParams.MATCH_PARENT;
|
272 | 276 | int height = (drawablePosition % 2 == 0) ? ViewGroup.LayoutParams.MATCH_PARENT : 0;
|
273 | 277 | layoutParamsText = new LinearLayout.LayoutParams(width, height);
|
274 |
| - layoutParamsImage = new LinearLayout.LayoutParams(width, height); |
| 278 | + layoutParamsImage = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT); |
275 | 279 | layoutParamsText.weight = (text != null) ? ((textWeight == 0) ? 1 : textWeight) : 0;
|
276 | 280 | textWeight = (int) layoutParamsText.weight;
|
277 |
| - layoutParamsImage.weight = (imageWeight == 0) ? 1 : imageWeight; |
278 |
| - imageWeight = (int) layoutParamsImage.weight; |
| 281 | + if (imageWeight > 0) { |
| 282 | + layoutParamsImage = new LinearLayout.LayoutParams(width, height); |
| 283 | + layoutParamsImage.weight = imageWeight; |
| 284 | + imageWeight = (int) layoutParamsImage.weight; |
| 285 | + } |
279 | 286 | textView.setLayoutParams(layoutParamsText);
|
280 | 287 | imageContainer.setLayoutParams(layoutParamsImage);
|
281 | 288 |
|
@@ -489,6 +496,42 @@ private void setImageColorStateList() {
|
489 | 496 | }
|
490 | 497 | }
|
491 | 498 |
|
| 499 | + private void setButtonLayoutParams() { |
| 500 | + if (drawableNormal != null && imageWeight == 0) |
| 501 | + getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { |
| 502 | + @Override |
| 503 | + public void onGlobalLayout() { |
| 504 | + LayoutParams layoutParams = (LayoutParams) imageContainer.getLayoutParams(); |
| 505 | + float scale; |
| 506 | + if (drawablePosition == LEFT || drawablePosition == RIGHT) { |
| 507 | + scale = ((float) container.getHeight()) / ((float) drawableNormal.getIntrinsicHeight()); |
| 508 | + layoutParams.width = (int) (scale * drawableNormal.getIntrinsicWidth()); |
| 509 | + layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; |
| 510 | + if (layoutParams.width > container.getWidth() / 2) { |
| 511 | + layoutParams.width = 0; |
| 512 | + layoutParams.weight = 1; |
| 513 | + imageWeight = 1; |
| 514 | + } else { |
| 515 | + if (drawablePosition == LEFT) |
| 516 | + ((LayoutParams) textView.getLayoutParams()).rightMargin = layoutParams.width; |
| 517 | + else |
| 518 | + ((LayoutParams) textView.getLayoutParams()).leftMargin = layoutParams.width; |
| 519 | + } |
| 520 | + } else { |
| 521 | + layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; |
| 522 | + layoutParams.height = 0; |
| 523 | + layoutParams.weight = 1; |
| 524 | + imageWeight = 1; |
| 525 | + } |
| 526 | + imageContainer.setLayoutParams(layoutParams); |
| 527 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) |
| 528 | + getViewTreeObserver().removeOnGlobalLayoutListener(this); |
| 529 | + else |
| 530 | + getViewTreeObserver().removeGlobalOnLayoutListener(this); |
| 531 | + } |
| 532 | + }); |
| 533 | + } |
| 534 | + |
492 | 535 | private Drawable changeDrawableColor(Bitmap bitmap, int color) {
|
493 | 536 | BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
|
494 | 537 | Paint paint = new Paint();
|
@@ -757,6 +800,7 @@ public void setImage(int position, Drawable drawableNormal, Drawable drawablePre
|
757 | 800 | imageContainer.setScaleType(scaleType);
|
758 | 801 |
|
759 | 802 | setContent();
|
| 803 | + setButtonLayoutParams(); |
760 | 804 | }
|
761 | 805 |
|
762 | 806 | /**
|
|
0 commit comments