Unfortunately Kirill didn't provide the src code so I did the ResizableButton code myself. Here's the src code.
The important part is the onLayout code:
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
final int oldWidth = getMeasuredWidth();
final int oldHeight = getMeasuredHeight();
int newWidth;
float size = mTextSize;
do {
setTextSize(size--);
measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
newWidth = getMeasuredWidth();
} while(newWidth > oldWidth);
setMeasuredDimension(oldWidth, oldHeight);
}
Some doubts:
Kirill mentions in his blog post:
As long as the measured width is less than available width – use the left and right parameters passed to onLayout, decrease the text size and repeat.
* It says less than when I am using greater than.
* I am decreasing the font size by 1. I don't know if that is the best way to do this.
What do you guys think?
I will try it out soon. :)
ReplyDeleteI tried this out and it works well for the most part. However, when resizing is not needed, the text on the button floats to the left.
ReplyDeleteAdding gravity should be enough to center the text.
ReplyDeleteI added gravity via setGravity and set it to center and it still didnt work. Not sure what the issue is. Very peculiar.
ReplyDeletehad same issue with it left aligning, added the line android:singleLine="true" and it centered.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete