From 94632a47ee9abba941dc1d791a47bc6c3a974404 Mon Sep 17 00:00:00 2001 From: ardocrat Date: Mon, 10 Apr 2023 22:05:07 +0300 Subject: [PATCH] fix: navigation bar height --- .../java/mw/gri/android/MainActivity.java | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/mw/gri/android/MainActivity.java b/app/src/main/java/mw/gri/android/MainActivity.java index a631ac2..325da69 100644 --- a/app/src/main/java/mw/gri/android/MainActivity.java +++ b/app/src/main/java/mw/gri/android/MainActivity.java @@ -1,9 +1,15 @@ package mw.gri.android; +import android.content.Context; +import android.graphics.Color; +import android.graphics.Point; +import android.os.Build; import android.os.Bundle; import android.system.ErrnoException; import android.system.Os; -import androidx.core.view.WindowCompat; +import android.view.Display; +import android.view.WindowInsets; +import android.view.WindowManager; import com.google.androidgamesdk.GameActivity; public class MainActivity extends GameActivity { @@ -19,7 +25,52 @@ public class MainActivity extends GameActivity { } catch (ErrnoException e) { throw new RuntimeException(e); } - WindowCompat.setDecorFitsSystemWindows(getWindow(), true); + super.onCreate(savedInstanceState); + + findViewById(android.R.id.content).setBackgroundColor(Color.BLACK); + findViewById(android.R.id.content).setPadding(0, 0, 0, getNavigationBarHeight()); + } + + public int getNavigationBarHeight() { + WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); + if (Build.VERSION.SDK_INT >= 30) { + return windowManager + .getCurrentWindowMetrics() + .getWindowInsets() + .getInsets(WindowInsets.Type.navigationBars()) + .bottom; + } else { + Point appUsableSize = getAppUsableScreenSize(this); + Point realScreenSize = getRealScreenSize(this); + + // navigation bar on the side + if (appUsableSize.x < realScreenSize.x) { + return appUsableSize.y; + } + + // navigation bar at the bottom + if (appUsableSize.y < realScreenSize.y) { + return realScreenSize.y - appUsableSize.y; + } + + // navigation bar is not present + return 0; + } + } + + public Point getAppUsableScreenSize(Context context) { + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + Point size = new Point(); + windowManager.getDefaultDisplay().getSize(size); + return size; + } + + public Point getRealScreenSize(Context context) { + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + Display display = windowManager.getDefaultDisplay(); + Point size = new Point(); + display.getRealSize(size); + return size; } } \ No newline at end of file