1

As you may know it too, you can change the widthPercent of PercentRelativeLayout in code just like this:

PercentRelativeLayout.LayoutParams params = ((PercentRelativeLayout.LayoutParams) view.getLayoutParams());
PercentLayoutHelper.PercentLayoutInfo paramsInfo = params.getPercentLayoutInfo();
paramsInfo.widthPercent = newWidthPercent; //e.g. change form 0.5 to 0.9
paramsInfo.aspectRatio = 1;

but in run time, you can see that the height will not change according to new width percent (e.g. here you expect equal width and height because of aspect ratio equal to 1 but the height remains the same)

so, how we achieve that?

Onik
  • 19,396
  • 14
  • 68
  • 91
Mostafa Aghajani
  • 1,844
  • 2
  • 14
  • 19

1 Answers1

0

I searched a lot in stackoverflow and google but can't find similar question and answer, so I decided to post it here as a Q/A for other developers facing the same problem. you should set the other attribute (height if you are setting widthPercent and width if you are setting heightPercent) of params variable to 0 (zero)! here is the complete solution:

PercentRelativeLayout.LayoutParams params = ((PercentRelativeLayout.LayoutParams) view.getLayoutParams());
PercentLayoutHelper.PercentLayoutInfo paramsInfo = params.getPercentLayoutInfo();
paramsInfo.widthPercent = newWidthPercent;
paramsInfo.aspectRatio = 1;
params.height = 0;

and it will update the height as we expect!

Mostafa Aghajani
  • 1,844
  • 2
  • 14
  • 19