Android - how to define a custom size form

Following the tips in a few other posts, I applied a round button that will be used in the application:

circular button

It is implemented using the XML selector:

<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Non focused states --> <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/big_ring_button_unfocused" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/big_ring_button_unfocused" /> <!-- Focused states --> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/big_ring_button_focused" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/big_ring_button_focused" /> <!-- Pressed --> <item android:state_pressed="true" android:drawable="@drawable/big_ring_button_pressed" /> </selector> 

The contents ... of an unallocated file (the rest just change colors):

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:innerRadius="@dimen/big_ring_button_inner_radius" android:thickness="@dimen/big_ring_button_thickness" android:useLevel="false"> <solid android:color="@color/white" /> </shape> 

I would like to use this template for all rounded buttons in my application, but as the text inside the buttons changes, the size of the button itself must change.

I thought I could do it programmatically, so I checked the GradientDrawable documentation - there is no method for changing the innerRadius attribute.

I currently have 4 XML files for each round button (selector, unfocused, focused and pressed), which is extremely ugly and will become a pain in the neck to maintain.

How can I make this button customizable in size (either XML or programmatically)?

thanks

+2
source share
2 answers

You cannot change the innerRadius attribute programmatically, since it only reads the XML attributes when inflated (see, for example, this answer ).

However, you can achieve more or less the same effect programmatically using a custom drawing method to draw a ring. For instance:

 public class RoundBorderDrawable extends GradientDrawable { private static final int RING_THICKNESS = 20; private static final int PADDING = 8; private static final int NORMAL_COLOR = Color.BLUE; private static final int PRESSED_COLOR = Color.RED; private Paint mPaint; public RoundBorderDrawable() { mPaint = new Paint(); mPaint.setStyle(Style.STROKE); mPaint.setStrokeWidth(RING_THICKNESS); mPaint.setColor(NORMAL_COLOR); } @Override public boolean isStateful() { return true; } @Override protected boolean onStateChange(int[] stateSet) { boolean result = super.onStateChange(stateSet); int color = NORMAL_COLOR; for (int i = 0; i < stateSet.length; i++) { if (stateSet[i] == android.R.attr.state_pressed) color = PRESSED_COLOR; } if (color != mPaint.getColor()) { mPaint.setColor(color); invalidateSelf(); result = true; } return result; } @Override public void draw(Canvas canvas) { super.draw(canvas); int diameter = Math.min(canvas.getWidth(), canvas.getHeight()) - RING_THICKNESS - 2 * PADDING; canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, diameter / 2, mPaint); } } 

This will draw a circle that fits in the dimensions of the Button (I assumed that you want to draw a circle even for non-square buttons).

Then simply set a new instance of RoundBorderDrawable as the background of your button (properties that are constants in this example can, of course, be supplied through the constructor or using setter methods).

+3
source

I have a better approach for this. Instead of Ring use Oval and give the stroke with white color and width @dimen/big_ring_button_thickness but this does not give you a round shape. To achieve a round shape, your button must be square, I mean that the width and height of the button must be the same.

0
source

Source: https://habr.com/ru/post/980551/


All Articles