views:

399

answers:

1

I'm probably just being daft but my Google searches are not working out well.

I have a bunch of buttons i add in code that all have dynamic text. I've set a background image for each of these buttons since the default greybutton doesn't work well for my application.

This works perfectly and when the text size (or content) changes, the button automatically grows to accommodate the expanded text. What doesn't work is that I'd like the button to scale proportionally - i.e. if the background image is round, i'd like it to stay round rather than oval as the button gets bigger.

With an imagebutton, there is a property "Adjust view bounds" that does exactly this but I cant put text on an imagebutton. Is there something equivalent for a regular button?

or am I going about this wrong?

i also tried setting the width of the button in code, but I can't seem to determine the new height (button.getHeight() returns 0)

+2  A: 

ok i found one way to do it... I modified the patch-9 to have its expandable area be the maximum on both axis.

then used the DisplayMetrics like this:

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        buttonView.setGravity(Gravity.CENTER);

        int buttonSize=(int) Math.floor(metrics.density*1.6*fontSize);
        buttonView.setWidth(buttonSize);
        buttonView.setHeight(buttonSize);

where fontSize is the size of the font in DIP that i'm placing on each button. In this case, since I only have a single letter on each button, i don't need to worry about the text length, but one could obviously tweak this to handle that situation as well.

jkhouw1