Skip to content

Commit 78d604d

Browse files
committed
Add centered postition of text in button.
1 parent 94baa91 commit 78d604d

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ Then select library folder from project. And start use. </p>
77
<p> >=API 8 </p>
88

99
<h2> Reference </h2>
10+
<p> Default image position is left and centered text in button. When image is bigger than half of width, weight automatically is setting to 1. <br>
11+
Only when image is in horizontal position text can be center. </p>
1012
<h3> XML tag's </h3>
1113

1214
This is the base tag of CustomButton:<br>
1315
```xml
14-
<pl.sigmapoint.customview.CustomButton<br>
16+
<pl.sigmapoint.customview.CustomButton
1517
android:id="@+id/sigmapoint_button"
1618
android:layout_width="match_parent"
1719
android:layout_height="match_parent"
1820
android:text="text"/>
1921
```
20-
<p> You can specify the following things:<br>
22+
<p> You can specify the following things:
2123
cb_primary_color - \/
2224
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>
2325
cb_background - background color of normal button {format - color} <br>
@@ -35,6 +37,7 @@ cb_text_padding_top - {format - dimension} <br>
3537
cb_text_padding_right - {format - dimension} <br>
3638
cb_text_padding_bottom - {format - dimension} <br>
3739
cb_text_weight - {format - integer} <br>
40+
cb_text_center or android:textAlignment - only works when image weight doesn't specify. Center text in button. {format - boolean or only "center" value} <br>
3841
<br>
3942
cb_shape_radius - corner radius {format - dimension}<br>
4043
cb_shape_type - shape type, you can choose: rect or oval <br>
@@ -83,6 +86,7 @@ setFrameColorDisabled(int color)
8386
setFrameSize(float frameSize)
8487
removeFrame()
8588
setText(String text)
89+
setTextCenter(boolean center)
8690
setTextColor(int color)
8791
setTextColorNormal(int color)
8892
setTextColorPressed(int color)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private void generate() {
243243
break;
244244
}
245245
}
246-
generatedCB.setImage(position, getResources().getDrawable(R.drawable.ic_owl), ImageView.ScaleType.FIT_CENTER, 1, null);
246+
generatedCB.setImage(position, getResources().getDrawable(R.drawable.ic_owl), ImageView.ScaleType.FIT_CENTER, 0, null);
247247
}
248248
generatedCB.setElevationEnabled(elevationTB.isChecked());
249249
if (getActivity() instanceof MainActivity) {

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.view.Gravity;
2424
import android.view.View;
2525
import android.view.ViewGroup;
26+
import android.view.ViewTreeObserver;
2627
import android.widget.ImageView;
2728
import android.widget.LinearLayout;
2829
import android.widget.TextView;
@@ -108,6 +109,7 @@ public CustomButton(Context context, ViewGroup.LayoutParams params, int primaryC
108109
else drawablePosition = -1;
109110

110111
setContent();
112+
setButtonLayoutParams();
111113

112114
setShapeBackground(); // set shape and backgroundColorNormal to button
113115
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) {
171173
imagePaddingArray[TOP] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_top, imagePadding);
172174
imagePaddingArray[RIGHT] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_right, imagePadding);
173175
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);
175177

176178
if (backgroundColorState != null) { // if backgroundColorNormal state is not null the set color from color state list to specific colors
177179
backgroundColorStateListToIntegers(backgroundColorState);
@@ -237,6 +239,8 @@ public CustomButton(Context context, AttributeSet attrs) {
237239
}
238240

239241
setOnClickListener(this);
242+
243+
setButtonLayoutParams();
240244
}
241245

242246
@Override
@@ -271,11 +275,14 @@ private void setContent() {
271275
int width = (drawablePosition % 2 == 0) ? 0 : ViewGroup.LayoutParams.MATCH_PARENT;
272276
int height = (drawablePosition % 2 == 0) ? ViewGroup.LayoutParams.MATCH_PARENT : 0;
273277
layoutParamsText = new LinearLayout.LayoutParams(width, height);
274-
layoutParamsImage = new LinearLayout.LayoutParams(width, height);
278+
layoutParamsImage = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
275279
layoutParamsText.weight = (text != null) ? ((textWeight == 0) ? 1 : textWeight) : 0;
276280
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+
}
279286
textView.setLayoutParams(layoutParamsText);
280287
imageContainer.setLayoutParams(layoutParamsImage);
281288

@@ -489,6 +496,42 @@ private void setImageColorStateList() {
489496
}
490497
}
491498

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+
492535
private Drawable changeDrawableColor(Bitmap bitmap, int color) {
493536
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
494537
Paint paint = new Paint();
@@ -757,6 +800,7 @@ public void setImage(int position, Drawable drawableNormal, Drawable drawablePre
757800
imageContainer.setScaleType(scaleType);
758801

759802
setContent();
803+
setButtonLayoutParams();
760804
}
761805

762806
/**

0 commit comments

Comments
 (0)