-1

I am trying to dynamically set the ImageView height in Xamarin droid project not in core.

LinearLayout layout4 = new LinearLayout(Activity);
layout4.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
layout4.SetGravity(GravityFlags.Center);
layout4.Orientation = Orientation.Vertical;

I am not able to set:

layout4.Height = 20;

Getting error: Property or indexer cannot be assigned, it is read only

pinedax
  • 9,246
  • 2
  • 23
  • 30
Devrath
  • 42,072
  • 54
  • 195
  • 297

2 Answers2

0

Use LayoutParams:

LinearLayout layout4 = new LinearLayout(this);
LinearLayout.LayoutParams parameters = new LinearLayout.LayoutParams(currentWidth, 20/*desired height*/);
layout4.LayoutParameters = parameters;
WMartin
  • 619
  • 7
  • 15
0

This is because the same as Java the height field of the LinearLayout is not accesible through the instance but with the LayoutParams.

You can set the desired height when creating the LayoutParams instance.

layout4.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, 20);

But, if you want to modify this value after the LayoutParams has been created and set you can do it like this:

layout4.LayoutParameters.Height = 20;
pinedax
  • 9,246
  • 2
  • 23
  • 30