Skip to content

Instantly share code, notes, and snippets.

@Nyconing
Last active December 13, 2018 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nyconing/ddf1e6bae7c59c3c46fb1fcec50fb878 to your computer and use it in GitHub Desktop.
Save Nyconing/ddf1e6bae7c59c3c46fb1fcec50fb878 to your computer and use it in GitHub Desktop.
Java class to Xamarin.Android port. Community feedback for Egis answer
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Java.Lang;
using Java.Lang.Reflect;
public static class NavigationbarSize {
public static Point getNavigationBarSize(Context context) {
var appUsableSize = getAppUsableScreenSize(context);
var realScreenSize = getRealScreenSize(context);
// navigation bar on the right
if (appUsableSize.X < realScreenSize.X) {
return new Point(realScreenSize.X - appUsableSize.X, appUsableSize.Y);
}
// navigation bar at the bottom
return (appUsableSize.Y < realScreenSize.Y)
? new Point(appUsableSize.X, realScreenSize.Y - appUsableSize.Y)
: new Point();
// navigation bar is not present
}
public static Point getAppUsableScreenSize(Context context) {
var windowManager = context.GetSystemService(Context.WindowService);
//var display = windowManager.DefaultDisplay;
var display = JNIEnv.CallObjectMethod(windowManager.Handle, JNIEnv.GetMethodID(JNIEnv.FindClass("android/view/WindowManagerImpl"), "getDefaultDisplay", "()Landroid/view/Display;"));
var size = new Point();
//display.GetSize(size);
JNIEnv.CallVoidMethod(display, JNIEnv.GetMethodID(JNIEnv.FindClass("android/view/Display"), "getSize", "(Landroid/graphics/Point;)V"), new JValue(size.Handle));
return size;
}
public static Point getRealScreenSize(Context context) {
var windowManager = context.GetSystemService(Context.WindowService);
//var display = windowManager.DefaultDisplay;
var display = JNIEnv.CallObjectMethod(windowManager.Handle, JNIEnv.GetMethodID(JNIEnv.FindClass("android/view/WindowManagerImpl"), "getDefaultDisplay", "()Landroid/view/Display;"));
var size = new Point();
if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBeanMr1) {
//display.GetRealSize(size);
JNIEnv.CallVoidMethod(display, JNIEnv.GetMethodID(JNIEnv.FindClass("android/view/Display"), "getRealSize", "(Landroid/graphics/Point;)V"), new JValue(size.Handle));
} else if (Build.VERSION.SdkInt >= BuildVersionCodes.IceCreamSandwich) {
try {
size.X = JNIEnv.CallIntMethod(context.Handle, JNIEnv.GetMethodID(JNIEnv.FindClass("android/view/Display"), "getRawWidth", "()I"));
size.Y = JNIEnv.CallIntMethod(context.Handle, JNIEnv.GetMethodID(JNIEnv.FindClass("android/view/Display"), "getRawHeight", "()I"));
//size.X = Display::class.java.getMethod("getRawWidth").invoke(display) as Int;
//size.Y = Display::class.java.getMethod("getRawHeight").invoke(display) as Int
} catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } catch (NoSuchMethodException e) { }
}
return size;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment