How to get Layouts Width and Height

Amir-JAmir-J USMember ✭✭

Hey guys, new in here, trying to get width and height of an existing layout. Googling it didn't really help. Here is the code:

LinearLayout mainLayout = (LinearLayout)FindViewById (Resource.Id.linearLayoutId);

int width = ?????????
int height = ?????????

There supposed to be a getWidth() and getHeight(), but can't figure it out.

Appreciate any suggestions.
A

Best Answer

Answers

  • DragosBarbuDragosBarbu DKUniversity ✭✭

    mainLayout.Width;
    It's C#/.Net way, so you don't have getters and setters, but you have properties

  • Amir-JAmir-J USMember ✭✭

    Hi, thanks,

    I tried that, yet its value is always ZERO:

    LinearLayout mainLayout = (LinearLayout)FindViewById (Resource.Id.linearLayoutId);

    Console.WriteLine ("Width is: " + mainLayout.Width.ToString ());

  • Amir-JAmir-J USMember ✭✭

    Thank you, that really helped, what I did is as following, just in case:

    mainLayout.ViewTreeObserver.GlobalLayout += (sender, e) =>
    {
        width = mainLayout.Width;
        height = width;
    

    }

  • razishrazish DEMember ✭✭

    @Amir-J said:
    Thank you, that really helped, what I did is as following, just in case:

    mainLayout.ViewTreeObserver.GlobalLayout += (sender, e) =>
    {
    width = mainLayout.Width;
    height = width;
    }

    Thank you for your hint, would you please explain how did you subscribe to ViewTreeObserver GlobalLayout?

  • Amir-JAmir-J USMember ✭✭
            someLayout.ViewTreeObserver.GlobalLayout += ViewTreeObserver_GlobalLayout;
    
                    while (something is false)
                    {
                        await Task.Delay(50);
                    }
                    someLayout.ViewTreeObserver.GlobalLayout -= ViewTreeObserver_GlobalLayout;
    
  • razishrazish DEMember ✭✭

    Thanks Amir jan

Sign In or Register to comment.