How to get the height of a framelayout ;

Shay Wilner 1,726 Reputation points
2021-11-11T20:24:33.843+00:00

Hello
I have a frame layout called gamearea;
I try to obtain its height but get 0;

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);


            gamearea.ViewTreeObserver.GlobalLayout += (sender, e) =>
            {
                float heightboard;
                heightboard = gamearea.Width * 1;
                var parameters = gamearea.LayoutParameters;
                parameters.Height = (int)heightboard;
                gamearea.LayoutParameters = parameters;
            };
            gamemanager = new Classmanagegame(8, gamearea.Height  );

When i check gamearea.Height it returns 0.

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,295 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2021-11-12T03:08:01.88+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can try to use AddOnGlobalLayoutListener to measture height of a framelayout,

       FrameLayout gamearea = FindViewById<FrameLayout>(Resource.Id.frameLayout1);  
         gamearea.ViewTreeObserver.AddOnGlobalLayoutListener(new MyOnGlobalLayoutListenter(gamearea));  
    

    Then achieve the MyOnGlobalLayoutListenter.cs.

       internal class MyOnGlobalLayoutListenter : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener  
           {  
         
               private FrameLayout frameLayout1;  
         
             
         
               public MyOnGlobalLayoutListenter(FrameLayout frameLayout1)  
               {  
                   this.frameLayout1 = frameLayout1;  
               }  
         
               public void OnGlobalLayout()  
               {  
                 int value= getViewHeight(frameLayout1);  
         
                   Console.WriteLine("Get the height of a framelayout"+ value);  
               }  
         
               int getViewHeight(View view)  
               {  
                  int WidthSpec= View.MeasureSpec.MakeMeasureSpec(0,MeasureSpecMode.Unspecified);  
                  int HeightSpec = View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified);  
                   View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified);  
                   view.Measure(WidthSpec, WidthSpec);  
                   int mesurewitdth=view.MeasuredHeight;  
         
                   return mesurewitdth;  
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.