How to get the height of a layout without declaring the height?


#1

Hi,

Is there any way to get the height of a layout without declaring a fixed height ? If I am trying to get the layout height without defining the height I am getting as “auto” but I want to know how much space(height) is is occupying.

Thanks.


Q: Get height of action bar as soon as drawn
#2

Use getActualSize()

yourView.getActualSize().height

#3

Thanks @manojdcoder for the response.
I tried but it is giving me ‘0’.


#4

I tried like :
var stackL = page.getViewById(“stackL”);
var height = stackL.getActualSize().height;

I am getting the height as ‘0’


#5

You must be trying before it is rendered on UI. Simply add a timeout (by then it will be rendered) and execute the same code.


#6

Thanks. by setting the timeout it is working.


#7

Don’t forget that there are three distinct events you will receive (and you can hook into) for every page when a page is loading - in this order:

  1. onNavigatingTo
  2. onLoaded
  3. onNavigatedTo

Even if your app is starting up and you load the default page, that page will also receive these events in that order.

What @manojdcoder suggested is correct. You can use yourView.getActualSize().height to measure the height/width of your layout, but you will get different results depending on which one of these events you are calling it from.

There’s a description about the layout and measuring process here: https://docs.nativescript.org/ui/layouts#layout-process

To make the long story short, your layout will be measured and laid out by the time you get to the final event onNavigatedTo. And so when you use yourView.getActualSize().height inside the onNavigatedTo event, you will get the correct size for your layout without the need for using any timeouts.

PS: It’s actually easy to see. You can setup to listen to all those three events for a page and inside each event do a simple console log of the width/height of your layout. You will see that in onNavigatingTo and onLoaded your layout’s dimensions are 0, but by the time your pages is fully loaded and setup in onNavigatedTo, your layout dimensions will correctly logged.


#8

Thanks @pentool.
That was a useful stuff.


#9

I realize this topic is somewhat old but for the record I had to still use a timeout using onNavigatedTo in iOS. Android worked as expected.


#10

Now you have layoutChanged event which guarantees that everything is in place.