How to get a set of buttons to be same width w/o hardcoding?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to get a set of buttons to be same width w/o hardcoding?

Postby henryci » Sat Mar 14, 2009 8:06 pm

I'm creating an app with 3 buttons, start, stop, and reset. They are all in a line and the centering would look a lot better and the app would be easier to use if all three buttons were the same width. However, I don't want to hardcode their width because if I make them all the width of the 'reset' button, then if I ever internationalize I'm potentially borked.

So, I tried having the layout XML have all 3 buttons use the longest string, and then had onCreate() rename the buttons but then they get resized. Similarly, if onCreate tries to use getWidth() to change the size later it fails because the buttons have not been drawn yet so their width is 0.

There are some terribly hacky ways to do this, such as creating a custom view, overriding its onSizeChanged and having it remember it's largest width and always maintain it. But there has to be a smarter way, which when I see will make me feel like an idiot.

So, I emplore: Make me feel dumb. :)

-- Henry
Posts: 4
Joined: Sat Mar 14, 2009 8:01 pm


Postby MrSnowflake » Sat Mar 14, 2009 9:30 pm

Give all 3 buttons: layout_weight="1" in the layout xml
User avatar
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium


Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 1 guest