Android: sizing programmatically according to screen size

Is it a bad habit to get the width and height of the device and accordingly set the program sizes of the images / buttons.

I find it inaccurate to use different folders for layouts and densities, as this gives me unexpected results on some devices (on top of errors)

Your experience is appreciated. Thanks you

+6
source share
2 answers

Yes, it is a very wired thing to make a layout for the entire supported Android screen. And there are many screen resolutions available in the market.

Once I made a demo, and it works for me. I made one button whose height and width are the same. Now I have set the required height and width according to one screen in which it looks perfect.

After that, I calculated the pixel that he should have made possible on this screen, and based on this I applied it to the entire screen.

Now it works perfectly in all devices with any density and resolution.

So, if there is any representation that is generated at runtime, and you want to set its height and width, the best way is to calculate its height ratio and use it.

Hope this helps you.

Enjoy the coding.,.

:)

+2
source

Well, in most cases, you will have layouts that become or become complex, and it will be difficult to program positions.

And this will also be a disadvantage supporting it, because you cannot use the interface material (graphic layout, etc.), and other people or by themselves will not understand the calculations in the same way as if they see representations in XML. Reorganization, change of place can be painful.

You will also often work with bitmaps that have a fixed size, if you calculate the sizes programmatically and stretch, they will not look good. At the very least, you will need a different set of bitmaps and loading accordingly.

This helps if, for example, you use relative layouts with rules (for example, above / align at the bottom of the parent, etc.), linear layouts with weights and dip (pixels independent of density). You should only make a software layout when it is not possible in a different way. Or in some specific cases when it really really makes the process easier.

+1
source

Source: https://habr.com/ru/post/922182/


All Articles