Monday, January 3, 2011


Some days ago I read Kirill Grouchnikov's blog post about custom components used in the new android market app.

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:

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 {

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?


  1. I 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.

  2. Adding gravity should be enough to center the text.

  3. I added gravity via setGravity and set it to center and it still didnt work. Not sure what the issue is. Very peculiar.

  4. had same issue with it left aligning, added the line android:singleLine="true" and it centered.

  5. This comment has been removed by the author.