0

I'm having trouble calculating the weight inside of an scroll view on my android app. I want the app's content to resize proportionally to the screen size. How can I calculate the weight inside a scroll view?

Here some of my code:

  LinearLayout layout_principal = (LinearLayout) findViewById(R.id.layout_items);// is horizontal layout and layout of Scrollview

  LinearLayout layout_titulo_opcion = new LinearLayout(this);
  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.FILL_PARENT,
          0,
          1f);
  layout_titulo_opcion.setLayoutParams(layoutParams);

  LinearLayout layout_titulo_opcion2 = new LinearLayout(this);
  LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.FILL_PARENT,
          0,
          1f);
  layout_titulo_opcion2.setLayoutParams(layoutParams2);

  layout_principal.addView(layout_titulo_opcion);
  layout_principal.addView(layout_titulo_opcion2);
Ravi
  • 34,851
  • 21
  • 122
  • 183
mhplur
  • 218
  • 1
  • 3
  • 16

1 Answers1

0

To get/set the weight, use this:

LinearLayout.LayoutParams mylp = (LinearLayout.LayoutParams)myLayout.getLayoutParams();
int myWeight = mylp.weight; // get weight
mylp.weight = 4; // set weight

To set the weight sum, use this:

myLayout.setWeightSum(10);
Jim
  • 10,172
  • 1
  • 27
  • 36
  • Thanks but i used this int scrollMainHeight = layout_principal.getWidth(); i take how reference the width of LinearLayout layout_principal – mhplur Jan 13 '14 at 22:45
  • getWidth() returns the space a layout is using. Weights are used to evenly distribute available space. e.g. two layouts with even weights will get half the available space each. If LayoutA weight=2 and LayoutB weight=1 then LayoutA will get twice as much space as LayoutB. This will resize proportionally to any screen, which seemed like what you were asking... – Jim Jan 13 '14 at 23:08
  • Thanks my friend for your comment – mhplur Jan 14 '14 at 14:26
  • i have another problem, i can't get the layout width in the oncreate function – mhplur Jan 17 '14 at 21:43
  • if you mean the width of the layouts that have weights, that's right. Essentially, they are not determined until they are on the screen. When I need to do this I either do it with a callback function like this: http://stackoverflow.com/questions/3779173/determining-the-size-of-an-android-view-at-runtime – Jim Jan 17 '14 at 23:07