T0bi Ethirbige
T0bi Ethirbige

Reputation: 43

How to calculate the size of a control without rendering it

I wanna create a view not in xaml but in c# with vertical and horizontal options set and get its size without rendering it or adding it to a content page. I know that I can't calculate the size of a view unless we add it to a layout but that also doesn't work because It wasn't rendered.

In conclusion, Xamarin doesn't set the bounds of a view unless It's rendered.

Upvotes: 0

Views: 185

Answers (1)

Anirudha Gupta
Anirudha Gupta

Reputation: 9299

You can MeasuredHeight property which can help you in your scenario.

var textView = new TextView(this);
textView.Text = "Hello";
textView.Measure(0, 0);
int height =  textView.MeasuredHeight;
int width =  textView.MeasuredWidth;

Upvotes: 1

Related Questions