0

I am working on a xamarin.forms application where I need to measure the height of the navigation bar. How do I measure it? I tried to follow this but I don't understand where to insert this code:

Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
    return resources.getDimensionPixelSize(resourceId);
}
return 0;
glennsl
  • 23,127
  • 11
  • 49
  • 65
sahithi
  • 869
  • 10
  • 26

1 Answers1

4

You are on the right track. The problem is that your app is cross platform and the navigation bar is a Android-only feature. Because you probably need to access the navigation bar height from within the shared code, you will need to use Dependency injection. This is thoroughly described here.

In short you will have to write a namespace like this:

public interface INavigationBarInfo
{
    int Height { get; };
}

And implement it for Android within the Android project:

public class NavigationBarInfo : INavigationBarInfo
{
    public int Height => RetrieveHeight();

    private void RetrieveHeight()
    {
       //android specific code to retrieve the navigation bar height
       //you can use the answer you linked to in your question          
    }
}
Martin Zikmund
  • 34,962
  • 7
  • 63
  • 83