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

Commit

Permalink
SegmentedButton's drawable positioning fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceryle committed Nov 20, 2016
1 parent bee7405 commit 5900574
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ private void getAttributes(AttributeSet attrs) {
private float buttonWeight;
private boolean hasButtonWidth, hasButtonWeight;

public void setDrawableTop(int drawableId) {
setCompoundDrawablesWithIntrinsicBounds(0, drawableId, 0, 0);
setImageTint(imageTint);
}

public void setDrawableBottom(int drawableId) {
setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, drawableId);
}

public void setDrawableLeft(int drawableId) {
setCompoundDrawablesWithIntrinsicBounds(drawableId, 0, 0, 0);
}

public void setDrawableRight(int drawableId) {
setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableId, 0);
}

public boolean hasButtonWidth() {
return hasButtonWidth;
}
Expand Down Expand Up @@ -197,7 +214,7 @@ private void drawButton() {
}

final int width = getWidth() - (getPaddingLeft() + getPaddingRight());
final int height = getWidth() - (getPaddingTop() + getPaddingBottom());
final int height = getHeight() - (getPaddingTop() + getPaddingBottom());

final Drawable[] drawables = getCompoundDrawables();

Expand All @@ -212,7 +229,7 @@ private void drawButton() {
offSet = (width - (textBounds.width() + drawableBounds.width()) + getRightPaddingOffset()) / 2 - getCompoundDrawablePadding();
break;
case 1:
offSet = (height - (textBounds.height() + drawableBounds.height()) + getBottomPaddingOffset()) / 2 + getCompoundDrawablePadding();
offSet = (height - (textBounds.height() + drawableBounds.height()) + getBottomPaddingOffset()) / 2 - getCompoundDrawablePadding();
break;
case 2:
offSet = ((textBounds.width() + drawableBounds.width()) - width + getLeftPaddingOffset()) / 2 + getCompoundDrawablePadding();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
sizeChanged = true;
}

String TAG = "ABCDEFG";

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Expand Down
28 changes: 0 additions & 28 deletions sample/src/main/java/co/ceryle/segmentedbutton/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,10 @@

public class MainActivity extends AppCompatActivity {

private Button button;
private SegmentedButtonGroup group;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

group = (SegmentedButtonGroup) findViewById(R.id.segmentedButtonGroup);
button = (Button) findViewById(R.id.button);

updateButton(group.getPosition());

group.setOnClickedButtonPosition(new SegmentedButtonGroup.OnClickedButtonPosition() {
@Override
public void onClickedButtonPosition(int position) {
updateButton(position);
}
});

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = group.getPosition();
position = ++position % 3;
updateButton(position);
group.setPosition(position, true);
}
});
}

private void updateButton(int position) {
button.setText("Position: " + position);
}
}
12 changes: 4 additions & 8 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@
<co.ceryle.segmentedbutton.SegmentedButton
android:layout_width="50dp"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/aragorn"
android:drawableTop="@drawable/aragorn"
android:padding="10dp"
app:sb_imageScale="0.4"/>

<co.ceryle.segmentedbutton.SegmentedButton
android:layout_width="100dp"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/gimli"
android:drawableTop="@drawable/gimli"
android:padding="10dp"
app:sb_imageScale="0.4"/>

<co.ceryle.segmentedbutton.SegmentedButton
android:layout_width="50dp"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/legolas"
android:drawableTop="@drawable/legolas"
android:padding="10dp"
app:sb_imageScale="0.4"/>
</co.ceryle.segmentedbutton.SegmentedButtonGroup>
Expand Down Expand Up @@ -211,7 +211,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/b1"
android:drawablePadding="4dp"
android:padding="10dp"
app:sb_imageScale="0.4"
app:sb_imageTint="@color/yellow_600"/>
Expand All @@ -220,7 +219,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/b2"
android:drawablePadding="4dp"
android:padding="10dp"
app:sb_imageScale="0.4"
app:sb_imageTint="@color/DarkRed"/>
Expand All @@ -247,15 +245,13 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/b17"
android:drawablePadding="4dp"
android:padding="10dp"
app:sb_imageScale="0.4"/>

<co.ceryle.segmentedbutton.SegmentedButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/b6"
android:drawablePadding="4dp"
android:padding="10dp"
app:sb_imageScale="0.4"/>

Expand Down Expand Up @@ -328,7 +324,7 @@
app:sbg_animateSelector="bounce"
app:sbg_animateSelectorDuration="1000"
app:sbg_backgroundColor="@color/black"
app:sbg_position="0"
app:sbg_position="2"
app:sbg_radius="2dp"
app:sbg_rippleColor="@color/white"
app:sbg_selectorColor="@color/white"
Expand Down

0 comments on commit 5900574

Please sign in to comment.