Get the height of the ContentPage in Xamarin Forms using CustomRenderer

617 views Asked by At

I want the height of the NavigationBar, tried using CustomRenderer but always gets the same value whether it is Tablet, Android phone.

I am using the following code

namespace OfflineFieldService.Droid
{
    public class CustomAndroidPageRenderer : PageRenderer
    {
        public CustomAndroidPageRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);
            var height = 0;
            var resources = Context.Resources;
            int resourceId = resources.GetIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0)
            {
                height = resources.GetDimensionPixelSize(resourceId);
            }
            var scale = Resources.DisplayMetrics.Density;
            var heightinUnits = (height - .5) / scale;
            App.screenHeight = heightinUnits;

        }
    }```

[Attached image in the link, since dont have permission to embed][1]


  [1]: https://i.stack.imgur.com/UosCb.png

In the image whatever marked in yellow, need to get the height of it. I am using Shell template.
1

There are 1 answers

0
Shubham Tyagi On

Navigation Bar Height iOS

public class NavRendererForiOS : NavigationRenderer
{
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        var height = NavigationBar.Bounds.Height;
    }
}

Nav Bar height android

public class NavRendererForAndroid : NavigationPageRenderer
{
    public CustomNaviForAndroid(Context context) : base(context)
    {

    }

    protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
    {
        base.OnElementChanged(e);

        var height = 0;

        Resources resources = Context.Resources;
        int resourceId = resources.GetIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0)
        {
            height = resources.GetDimensionPixelSize(resourceId);
        }
    }
}

Screen Size Android

App.ScreenHeight = (int) (Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
App.ScreenWidth = (int) (Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);

Screen Size iOS

App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;