3

tl/dr

I am trying to change some layouts sizes in code, and I can change some of them, but the other ones that needed to update based on the changes won't update together. Instead, they retain the previous width that the changed ones had.

So, I am fooling around with Android Studio and trying to change the size of some LinearLayouts Programatically.

This is what I'm doing:

final LinearLayout ll3 = (LinearLayout)findViewById(R.id.box3);
ViewGroup.LayoutParams p3 = ll3.getLayoutParams();
int w3 = 5;
float aux = (float)w3/15;
p3.width = Math.round(max*aux);
ll3.setLayoutParams(p3);

and then this is what I'm getting:

Log.i("OnLongClick p3_width", Integer.toString(p3.width));
Log.i("OnLongClick ll3_getWidth", Integer.toString(ll3.getWidth()));

OnLongClick p3_width﹕ 179

OnLongClick ll3_getWidth﹕ 224

The ll3.getWidth() is returning the previous value, which ll3 HAD before setting the new parameters. If I repeat the process, then it will display something like:

OnLongClick p3_width﹕ 314 //New Value

OnLongClick ll3_getWidth﹕ 179 //Old (previous) Value

I need the ll3.getWidth() to be the same as p3.width, because I'm calling it in another function in order to recalculate the other LinearLayouts that my app has. Otherwise, when the app redraws it, the rectangles changed manually will change, but the other ones (which uses the changes to modify itself) will remain the way that the changed ones were previously (sorry about this sentence).

I'm thinking that this happens because the setLayoutParams must be doing its job further in time, and not before I call getWidth(), but I have no clue if that is trully the problem, and how to fix it.

Any help is apreciated.

Thanks in advance!!!

EDIT2: If I send

openOptionsMenu();
closeOptionsMenu();

It fixes the size of the rectangles. However, it presents a delay doing that, so it is reeeally ugly. Anyway, I would need to place whatever is in the middle of openOptionsMenu() to fix that (closeOptionsMenu() doesnt change the view, only closes the menu).

GuiFGDeo
  • 706
  • 1
  • 8
  • 29

2 Answers2

3

Found an answer!!! And as in a lot of times, it was right there in my face.

What was happening is that p3.width is nothing more than ll3.getLayoutParams().width. So, instead of calling ll3.getWidth() (which returned a wrong width) I am now calling ll3.getLayoutParams().width, and then it will correctly make the calculations.

I still don't know why ll3.getWidth() will return a wrong value, but at least it is working (still, if you know why, please let me know as well!!!).

GuiFGDeo
  • 706
  • 1
  • 8
  • 29
1

Step #1: Get the LinearLayout's LayoutParams via getLayoutParams(). The actual class for these will depend on the LinearLayout's parent.

Step #2: Modify values in the LayoutParams.

Step #3: Set the LinearLayout's modified LayoutParams via setLayoutParams().

Hardik
  • 56
  • 3
  • Step#1 is ViewGroup.LayoutParams p3 = ll3.getLayoutParams(); Step#2 is p3.width = Math.round(max*aux); Step#3 is ll3.setLayoutParams(p3); I am already doing everything – GuiFGDeo Feb 04 '15 at 13:34