Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Adjust sizes programmatically according to screen size

Is it a bad habit to get the width and hight of the device and set images/button sizes programmatically accordingly.

I find it inaccurate to use different folders for layouts and densities as it gives me wierd results on some devices (on top of the inacurancies)

Your experience is appreciated. Thank you

like image 924
Snake Avatar asked Aug 05 '12 23:08

Snake


People also ask

How do I deal with different screen sizes on Android?

The best way to create a responsive layout is to use ConstraintLayout as the base layout in your UI. ConstraintLayout enables you to specify the position and size of each view according to spatial relationships with other views in the layout. All the views can then move and resize together as the screen size changes.

How do I scale my Android screen?

Open up Settings and then tap on Display. Scroll down and tap on Display size. In this new screen, drag the slider to left to shrink the display size or the right to enlarge it. They've even included a sample app so you can see how the resizing will affect both text and on-screen elements.

How can get screen width and height in Android programmatically?

1. Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size.


2 Answers

Yes it is very wired thing to make the layout for the all supported screen of android. And there are lots of screen resolution available in market.

Once i have made a Demo and it Works for me. I have made one Button which height and Width is same. Now i have set its required height and width as per the one Screen in which it is looking perfect.

After that i have calculated the pixel that it required to make it Possible in that screen and based on that i have applied it to all screen.

Now it works great in all device with any density and resolution.

So if there is any view that generate at run time and you want to set its height and width then the best way is to calculate its height-width ratio and use it.

hope it will helps you.

Enjoy Coding. . . .

:)

like image 103
Shreyash Mahajan Avatar answered Sep 30 '22 20:09

Shreyash Mahajan


Well, most of cases you will have layouts which are, or will become, complex, and it will be difficult to calculate the positions programmatically.

And it will be also a disadvantage mantaining it, because you will not be able to use the interface stuff (grafic layout and so on), and other people, or yourself, will not understand the calculations the same way they would if they see the views in XML. Reorganizing, changing somewhere a position could be painful.

You also will be working frequently with bitmaps, which have a fixed size, if you calculate the dimensions programmatically and stretch they will not look good. At least you would need different set of bitmaps and load accordingly.

It helps if you for example use relative layouts with rules (like above of / align at the bottom of the parent, etc), linear layouts with weights, and dip (density independent pixels). You should do programmatic layout only when it's not possible in other way. Or in some certain cases where it really-really makes things easier.

like image 45
User Avatar answered Sep 30 '22 18:09

User