tags:

views:

30

answers:

1

Hi!

i' making a remote-app, so i need a varying number of buttons, grouped by tableviews and tablerows.

i can create the fix ones (that must alwasy be available) at designtime via xml, making use of the layouts making all buttons fit on every resolution.

but there are buttons i have to create at runtime. to make them behave the same way as the fixed ones, i need to assign the layout width, -height and -weight values.

i was searching the whole documentation, but there it's alwas done in xml, no word about runtime / matching routines.

is this even possible?? how else could i create a to-all-sides-fitting grid of buttons?

thanks a lot!

A: 

Button should inherit setWidth(int pixels) from TextView (since it extends it). Have you tried using that? E.g.:

myButton.setWidth(100);
myButton.setHeight(50);
//Now myButton is 100px wide and 50px tall

Make sure you expand the drop down for "Inherited Methods" when you're looking for methods on the Button page in the docs. You may also want to expand "Inherited Attributes".

Edit: Okay, I'll bite - if you want to retain screen independence you'll need to use dip units. The docs discuss this here, but it would be something like the following:

//Find screen density scale factor
final float scale = getContext().getResources().getDisplayMetrics().density;

int width = (int)(100 * scale) // Or whatever width you want
int height = (int)(50 * scale) // Or whatever height you want
myButton.setWidth(width);
myButton.setHeight(height);

Note: This is an example. This is not intended to be production code. You probably don't have a button named myButton. Please adapt this to your specific needs or read the linked documentation for more help, etc.

eldarerathis
Don't use absolute pixels
Falmarri
@Falmarri: I'm going to assume you mean that dip units are the best way to do this, though your comment is really too vague to be particularly helpful so I could be completely wrong. In any case, I'll add some scaling code, which I originally omitted due to the trivial nature of the question. I'd also suggest, though, that you consider posting more helpful comments, since one-off "don't do this" comments really don't contribute to the discussion, but that's up to you.
eldarerathis