简体   繁体   中英

How to get Navigation bar Height in Xamarin Android?

how to get top navigation bar Height in Xamarin Android? below is the java code

public static int getSceenHeight(Activity activity) {
   //obsolete now
    return activity.getWindowManager().getDefaultDisplay().getHeight()+getNavigationBarHeight(activity);
}

 public static int getNavigationBarHeight(Activity activity) {
    if (!isNavigationBarShow(activity)){
        return 0;
    }
    Resources resources = activity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height",
            "dimen", "android");
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}

public static boolean isNavigationBarShow(Activity activity){

        Display display = activity.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        Point realSize = new Point();
        display.getSize(size);
        display.getRealSize(realSize);
        return realSize.y!=size.y;

    }
}

how to apply this in Xamarin Android ?

Seem like there is this forever ongoing confusion about what is called a NavigationBar when it comes to Xamarin/Xamarin.Form and Android. I really wish that Xamarin would fix this naming so that it is in line with android.

Anyway, Calling ...

int resourceId = resources.GetIdentifier("navigation_bar_height",
            "dimen", "android"); 

...is going to return the value for the bottom NavigationBar(Android)(the buttons that is part of the OS). From the comments above it seems like you are looking for the Xamarin NavigationBar(The Top one that contains menu Title and so on (Bellow status bar)).

Please try the following

var styledAttributes = Context.Theme.ObtainStyledAttributes(new[] { Android.Resource.Attribute.ActionBarSize });
var actionBarSize = (int)styledAttributes.GetDimension(0, 0);
styledAttributes.Recycle();

Thanks to Fred for his post here

I have no idea how you gonna use it, as you said I have converted above you methods to C#

public static int getSceenHeight(Activity activity)
{
    //obsolete now
    return activity.WindowManager.DefaultDisplay.Height + getNavigationBarHeight(activity);
}

public static int getNavigationBarHeight(Activity activity)
{
    if (!isNavigationBarShow(activity))
    {
        return 0;
    }
    Resources resources = activity.Resources;
    int resourceId = resources.GetIdentifier("navigation_bar_height",
            "dimen", "android");
    int height = resources.GetDimensionPixelSize(resourceId);
    return height;
}

public static bool isNavigationBarShow(Activity activity)
{
    if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBeanMr1)
    {
        Display display = activity.WindowManager.DefaultDisplay;
        Point size = new Point();
        Point realSize = new Point();
        display.GetSize(size);
        display.GetRealSize(realSize);
        return realSize.Y != size.Y;
    }
    else
    {
        bool menu = ViewConfiguration.Get(activity).HasPermanentMenuKey;
        bool back = KeyCharacterMap.DeviceHasKey(Keycode.Back);
        if (menu || back)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

The method to get status bar(top one that displays signal, battery etc as you said)

private int getStatusBarHeight()
{
    Resources res = this.Resources;
    int resourceId = res.GetIdentifier("status_bar_height", "dimen", "android");
    int height = res.GetDimensionPixelSize(resourceId);
    return height;
}

The method to get navigation bar
        private int getNavigationBarHeight()
{
            Resources res = this.Resources;
            int resourceId = res.GetIdentifier("navigation_bar_height", "dimen", "android");
            int height = res.GetDimensionPixelSize(resourceId);
            return height;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM