Reputation: 147
How do I get the height of a gridview that I'm implementing? Simply calling gridview.Height returns a 0, since the grid hasn't been drawn at the time I called it, thus after looking around I learnt that I have to implement a view tree observer. I did manage to get the height successfully, but I'm unable to use it due being unable to stop the view tree observer and retrieve the height. I've also found this thread, but it is unresolved.
Here is my code:
// Get gridview height
ViewTreeObserver vto = gridview.ViewTreeObserver;
vto.GlobalLayout += (sender, args) =>
{
if (GRID_HEIGHT > 0)
{
GRID_HEIGHT = gridview.Height;
// Configure gridview
gridview.Adapter = new CustomAdapter(this);
return;
}
};
When the code is executed, it just loops on forever. How can I exit the loop, or is there another way to get the height of the gridview? Any help is greatly appreciated.
Upvotes: 2
Views: 548
Reputation: 7850
You can use the Post
method in any view like:
int height = 0;
gridview.Post(() =>
{
height = grid.Height;
});
Upvotes: 1