Skip to content
This repository was archived by the owner on Mar 27, 2022. It is now read-only.

Commit cb05f55

Browse files
committed
Add support for setting selector width
1 parent 96b84ba commit cb05f55

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

library/src/main/java/co/ceryle/radiorealbutton/RadioRealButtonGroup.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private void setBorderAttrs() {
223223
animateDrawablesEnterDuration, animateTextsEnterDuration, animateSelector, animateSelectorDuration, animateSelectorDelay, animateDrawablesExit,
224224
animateDrawablesExitDuration, animateTextsExit, animateTextsExitDuration, lastPosition, buttonPadding,
225225
buttonPaddingLeft, buttonPaddingRight, buttonPaddingTop, buttonPaddingBottom, groupBackgroundColor,
226-
dividerPadding, dividerSize, dividerRadius, bottomLineSize, bottomLineRadius, selectorSize, selectorRadius, initialPosition,
226+
dividerPadding, dividerSize, dividerRadius, bottomLineSize, bottomLineRadius, selectorSize, selectorWidth, selectorRadius, initialPosition,
227227
selectorDividerSize, selectorDividerRadius, selectorDividerColor, selectorDividerPadding, checkedButtonId,
228228
animateTextsColorExit, animateTextsColorEnter, animateTextsColorDuration, animateTextsColorDurationExit, animateTextsColorDurationEnter,
229229
animateDrawablesTintExit, animateDrawablesTintEnter, animateDrawablesTintDuration, animateDrawablesTintDurationExit, animateDrawablesTintDurationEnter;
@@ -233,7 +233,7 @@ private void setBorderAttrs() {
233233
private boolean bottomLineBringToFront, selectorBringToFront, selectorAboveOfBottomLine, selectorTop, selectorBottom, hasPadding,
234234
hasPaddingLeft, hasPaddingRight, hasPaddingTop, hasPaddingBottom, clickable, enabled,
235235
enableDeselection, hasEnabled, hasClickable, hasBorder, hasAnimateDrawables, hasAnimateTexts, hasAnimation, selectorFullSize,
236-
hasAnimateTextsColor, hasAnimateDrawablesTint;
236+
hasAnimateTextsColor, hasAnimateDrawablesTint, hasSelectorWidth;
237237

238238
private void getAttributes(AttributeSet attrs) {
239239
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.RadioRealButtonGroup);
@@ -262,6 +262,8 @@ private void getAttributes(AttributeSet attrs) {
262262
selectorBringToFront = ta.getBoolean(R.styleable.RadioRealButtonGroup_rrbg_selectorBringToFront, false);
263263
selectorAboveOfBottomLine = ta.getBoolean(R.styleable.RadioRealButtonGroup_rrbg_selectorAboveOfBottomLine, false);
264264
selectorSize = ta.getDimensionPixelSize(R.styleable.RadioRealButtonGroup_rrbg_selectorSize, 12);
265+
selectorWidth = ta.getDimensionPixelSize(R.styleable.RadioRealButtonGroup_rrbg_selectorWidth, 0);
266+
hasSelectorWidth = ta.hasValue(R.styleable.RadioRealButtonGroup_rrbg_selectorSize);
265267
selectorRadius = ta.getDimensionPixelSize(R.styleable.RadioRealButtonGroup_rrbg_selectorRadius, 0);
266268

267269
animateSelector = ta.getInt(R.styleable.RadioRealButtonGroup_rrbg_animateSelector, 0);
@@ -414,7 +416,18 @@ private void createSelectorItem(int position, RadioRealButton button) {
414416
int height = selectorSize;
415417
if (selectorFullSize)
416418
height = LayoutParams.MATCH_PARENT;
417-
view.setLayoutParams(new LinearLayout.LayoutParams(0, height, 1));
419+
420+
if (hasSelectorWidth) {
421+
view.setLayoutParams(new LinearLayout.LayoutParams(selectorWidth, height));
422+
} else {
423+
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));
424+
}
425+
426+
LinearLayout wrapperView = new LinearLayout(getContext());
427+
wrapperView.setLayoutParams(new LinearLayout.LayoutParams(0, height, 1));
428+
wrapperView.setOrientation(LinearLayout.HORIZONTAL);
429+
wrapperView.setGravity(Gravity.CENTER);
430+
wrapperView.addView(view);
418431

419432
int value = 0;
420433
if (position == lastPosition) {
@@ -423,27 +436,27 @@ private void createSelectorItem(int position, RadioRealButton button) {
423436

424437
switch (animationType) {
425438
case ANIM_SCALE_X:
426-
view.setScaleX(value);
439+
wrapperView.setScaleX(value);
427440
break;
428441
case ANIM_SCALE_Y:
429-
view.setScaleY(value);
442+
wrapperView.setScaleY(value);
430443
break;
431444
case ANIM_TRANSLATE_X:
432445
if (value == 0)
433-
view.setVisibility(INVISIBLE);
446+
wrapperView.setVisibility(INVISIBLE);
434447
break;
435448
case ANIM_TRANSLATE_Y:
436-
view.setTranslationY(value == 1 ? value : selectorSize);
449+
wrapperView.setTranslationY(value == 1 ? value : selectorSize);
437450
break;
438451
case ANIM_ALPHA:
439-
view.setAlpha(value);
452+
wrapperView.setAlpha(value);
440453
break;
441454
}
442455
button.setOnSelectorColorChangedListener(this, position);
443-
updateViewSelectorColor(view, button.hasSelectorColor() ? button.getSelectorColor() : selectorColor);
456+
updateViewSelectorColor(wrapperView, button.hasSelectorColor() ? button.getSelectorColor() : selectorColor);
444457

445-
v_selectors.add(view);
446-
selectorContainer.addView(view);
458+
v_selectors.add(wrapperView);
459+
selectorContainer.addView(wrapperView);
447460
}
448461

449462
private void initButtonListener(RadioRealButton button, final int position) {
@@ -1051,7 +1064,7 @@ public void onSelectorColorChanged(int position, int selectorColor) {
10511064

10521065
private void updateViewSelector(View view, int color, int radius, int size) {
10531066
RoundHelper.makeRound(
1054-
view,
1067+
((LinearLayout) view).getChildAt(0),
10551068
color,
10561069
radius,
10571070
selectorFullSize ? null : size

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<attr name="rrbg_selectorBottom" format="boolean"/>
109109
<attr name="rrbg_selectorColor" format="color"/>
110110
<attr name="rrbg_selectorSize" format="dimension"/>
111+
<attr name="rrbg_selectorWidth" format="dimension" />
111112
<attr name="rrbg_selectorBringToFront" format="boolean"/>
112113
<attr name="rrbg_selectorAboveOfBottomLine" format="boolean"/>
113114
<attr name="rrbg_selectorRadius" format="dimension"/>

0 commit comments

Comments
 (0)