Reputation: 669
I'm developing an app that requires circle buttons, I achieved the expected result, like this image:
The problem is that I need to set a fixed value to the width and height to get the correct shape, otherwise the buttons would be like the following image and I needed it to be dynamic:
My button's XML:
<Button
android:id="@+id/bt_ab_mais"
style="@style/TipoSanguineoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="13dp"
android:text="@string/pedir_doacao_sangue_ab_mais"
android:textAllCaps="true"
android:textSize="15sp"
android:textStyle="bold" />
The button's background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<stroke android:width="2dp" android:color="@android:color/darker_gray" />
<solid android:color="@color/branco" />
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<stroke android:width="2dp" android:color="@android:color/darker_gray" />
<solid android:color="@color/cinza_tablayout" />
</shape>
</item>
</selector>
Does anybody know how to do it?
Upvotes: 1
Views: 1223
Reputation: 669
I got the answer, I just needed to create a custom button, where I override the onMeasure
method, like this:
public class CircleButton extends Button {
public CircleButton(Context context) {
super(context);
}
public CircleButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);
}
}
The button's XML:
<br.org.component.CircleButton
android:id="@+id/bt_b_menos"
style="@style/TipoSanguineoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="13dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:stateListAnimator="@null"
android:text="@string/pedir_doacao_sangue_b_menos"
android:textAllCaps="true"
android:textSize="15sp"
android:textStyle="bold" />
Upvotes: 1