0

My question is very simple. Which is more efficient: setting a views width's or height's percentage from JAVA code, by getting the phones pixel count and calculating the number of pixels needed for the desired width or height, or put everything in a LinearLayout and set the weight of the view.

NOTE: Sometimes the views need to have margins, which are also specified in percentages.

Hampel Előd
  • 405
  • 2
  • 8
  • 19

3 Answers3

0

I would prefer doing it through Linearlayout adding weight to child elements add you can easily and margin to it.

Fahim
  • 12,198
  • 5
  • 39
  • 57
0

Hey use this code context.getResources().getDisplayMetrics().widthPixels to get the width of your screen and context.getResources().getDisplayMetrics().heightPixels to get height and divide them in the desired ratio and set it to the view.

Hope this is what you want.

Nitesh
  • 3,868
  • 1
  • 20
  • 26
0

I think the weight is best for comparative distribution of views within LinearLayout.

its very flexible to use ..you can use it for the distribution of extra space within child or also create the weight oriented layout by setting the views height or width(whichever requried by you) to zero .

Nested weight are bad for performance

K Guru
  • 1,292
  • 2
  • 17
  • 36
  • Both of them are very flexible. JAVA code needs more coding, LinearLayout is more simple to write down, but my question was: which is more efficient? – Hampel Előd Feb 24 '15 at 08:44