6

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

Snake
  • 14,228
  • 27
  • 117
  • 250

2 Answers2

2

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. . . .

:)

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • I love this practical comment. That definitely makes sense. I understand the ratio aspect but how do u determine the width in the first place? What I do is I try to see how many pixels is required for the button to look good. Then I see the percentage it takes from the screen total pixels. Finally I use this percentage to calculate the button width on every screen programmatically. Does it make sense? – Snake Aug 06 '12 at 07:19
  • Yes it will Works. Just set the Button Height and width programetically for one screen. Once if it looks Nice then count the Height and Width Ration based on that device Screen resolution. After getting the ration. Use that ration in java file. It works for all Screen Size. – Shreyash Mahajan Aug 06 '12 at 07:27
  • how you calculate the pixels of button?? can you provide code for that piece of work?? help will be appreciated – User42590 Jan 01 '13 at 07:28
  • @Akhter: Right now i am not able to give you code. But yes can give you idea. First make layout that fit for your screen. then measure the button height-width with that specific height-width and also calculate the device height-width. Now, you got the ration of the button height-width compare to that device height-width. Now set it dynamically to get the device height-width and set button's height-width dynamically based on the rasio calculation. Hope you got my point. – Shreyash Mahajan Jan 01 '13 at 07:34
  • @Akhter: Right now i am not able to give you code. But yes can give you idea. First make layout that fit for your screen. then measure the button height-width with that specific height-width and also calculate the device height-width. Now, you got the ration of the button height-width compare to that device height-width. Now set it dynamically to get the device height-width and set button's height-width dynamically based on the ratio calculation. Hope you got my point. – Shreyash Mahajan Jan 01 '13 at 07:34
  • Display display = getWindowManager().getDefaultDisplay(); Point size= new Point(); (display).getSize(size); int width = size.x; int height = size.y; mybutton=(Button) findViewById(R.id.Send); Toast.makeText(this, "width"+mybutton.getWidth()+"\t"+"height"+mybutton.getHeight(), Toast.LENGTH_SHORT).show(); – User42590 Jan 01 '13 at 07:42
  • i toast the width and height of button but it gives 0 height and 0 width – User42590 Jan 01 '13 at 07:44
  • @Akhter: Right now i am not able to give you code. But yes can give you idea. First make layout that fit for your screen. then measure the button height-width with that specific height-width and also calculate the device height-width. Now, you got the ration of the button height-width compare to that device height-width. Now set it dynamically to get the device height-width and set button's height-width dynamically based on the ratio calculation. Hope you got my point. – Shreyash Mahajan Jan 01 '13 at 08:12
1

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.

User
  • 31,811
  • 40
  • 131
  • 232
  • That's true. But doing that made me run into the following question that I created with a bounty. http://stackoverflow.com/questions/10378030/two-devices-similar-densities-yet-different-image-sizes – Snake Aug 06 '12 at 07:22