Forum Xamarin.Android

How to add children at runtime in any X Y position from code behind?

I'm currently having dynamic content properties which will be downloaded from server such as width, height, X and Y position. To do this, I believe I need to use RelativeLayout(correct me if I'm wrong). So, I will loop object list that contains each element properties and add it on run-time. But, I'm confused on using the overload parameter on ".Children.Add" function. My emulator running on 1080p (1920p x 1080p) but when I test to create grid children with size of 500p x 500p, it utilize almost 85% of view height. As per below.

"MasterLayout" is RelativeLayout name:-

Device.BeginInvokeOnMainThread(() =>
{
    // Get Metrics
    var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
    // Orientation (Landscape, Portrait, Square, Unknown)
    var orientation = mainDisplayInfo.Orientation;

    // Rotation (0, 90, 180, 270)
    var rotation = mainDisplayInfo.Rotation;

    // Width (in pixels)
    var width = mainDisplayInfo.Width;

    // Height (in pixels)
    var height = mainDisplayInfo.Height;

    // Screen density
    var density = mainDisplayInfo.Density;

    MasterLayout.Children.Add(new Grid
    {
        BackgroundColor = Color.Blue,
        WidthRequest = 500,
        HeightRequest = 500,
    }
    , Constraint.Constant(0));
});

Then, I try to get MasterLayout size to clarify, and I get Width:960, Height:540. I cannot change the MasterLayout size as it is read-only. Why is that happening?
How to view content as per original resolution of 1920x1080?

Q1: How to set the display content element to use real 1920x1080 size?
Q2: How to set the position X Y inside MasterLayout?

My content will be running on Android Player.

Best Answer

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited October 2019 Accepted Answer

    How to set the display content element to use real 1920x1080 size?

    You cannot get the whole size because the status Bar and navigation bar(Home button) will take up part of the space. And setting WidthRequest to 500 is different from 500dp in Android.

    How to set the position X Y inside MasterLayout?

    layout.Children.Add(box, Constraint.RelativeToParent((parent) =>
        {
          return (.5 * parent.Width) - 100;
        }),
        Constraint.RelativeToParent((parent) =>
        {
            return (.5 * parent.Height) - 100;
        }),
        Constraint.Constant(50), Constraint.Constant(50));
    

    Check the Tutorial:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/relative-layout

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited October 2019 Accepted Answer

    How to set the display content element to use real 1920x1080 size?

    You cannot get the whole size because the status Bar and navigation bar(Home button) will take up part of the space. And setting WidthRequest to 500 is different from 500dp in Android.

    How to set the position X Y inside MasterLayout?

    layout.Children.Add(box, Constraint.RelativeToParent((parent) =>
        {
          return (.5 * parent.Width) - 100;
        }),
        Constraint.RelativeToParent((parent) =>
        {
            return (.5 * parent.Height) - 100;
        }),
        Constraint.Constant(50), Constraint.Constant(50));
    

    Check the Tutorial:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/relative-layout

  • luieyluiey Member ✭✭

    Hi, I can get display full screen because on my MainActivity, I have declared:-

    //Remove title bar
    RequestWindowFeature(WindowFeatures.NoTitle);
    //Remove notification bar
    Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
    //Wake screen when app is added to task
    Window.SetFlags(WindowManagerFlags.TurnScreenOn, WindowManagerFlags.TurnScreenOn);
    //Keep screen on without timeout
    Window.SetFlags(WindowManagerFlags.KeepScreenOn, WindowManagerFlags.KeepScreenOn);
    
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
    {
        var stBarHeight = typeof(Xamarin.Forms.Platform.Android.FormsAppCompatActivity).GetField("statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        if (stBarHeight == null)
        {
            stBarHeight = typeof(Xamarin.Forms.Platform.Android.FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        }
        stBarHeight?.SetValue(this, 0);
    }
    

    And your .5 * parentWidth did the trick. Awesome!

    Currently, I'm using below code based from what I found on forums/discussion:-

    MasterLayout.Children.Add(gridChild,
                                    xConstraint: Constraint.Constant(100 * .5),
                                    yConstraint: Constraint.Constant(100 * .5),
                                    widthConstraint: Constraint.Constant(500 * .5),
                                    heightConstraint: Constraint.Constant(500 * .5));
    

    But I want to try yours. Thanks Jarvan

Sign In or Register to comment.