From 2cb1dea576c2bb220b9a8a18e97028200014297d Mon Sep 17 00:00:00 2001 From: ardocrat Date: Mon, 10 Apr 2023 23:42:45 +0300 Subject: [PATCH] feat: navigation bar color (not working with NativeActivity :() --- .../java/mw/gri/android/MainActivity.java | 8 +- app/src/main/java/mw/gri/android/Utils.java | 75 +++++++++++++++++++ app/src/main/res/values/colors.xml | 7 +- app/src/main/res/values/themes.xml | 21 +++--- 4 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/mw/gri/android/Utils.java diff --git a/app/src/main/java/mw/gri/android/MainActivity.java b/app/src/main/java/mw/gri/android/MainActivity.java index 15f9190..915ac11 100644 --- a/app/src/main/java/mw/gri/android/MainActivity.java +++ b/app/src/main/java/mw/gri/android/MainActivity.java @@ -1,12 +1,9 @@ package mw.gri.android; import android.app.NativeActivity; -import android.content.res.Configuration; import android.os.Bundle; import android.system.ErrnoException; import android.system.Os; -import android.util.Log; -import android.view.WindowManager; public class MainActivity extends NativeActivity { @@ -21,6 +18,11 @@ public class MainActivity extends NativeActivity { } catch (ErrnoException e) { throw new RuntimeException(e); } + super.onCreate(savedInstanceState); + + int navBarHeight = Utils.getNavigationBarHeight(getApplicationContext()); +// int statusBarHeight = Utils.getStatusBarHeight(getApplicationContext()); + findViewById(android.R.id.content).setPadding(0, 0, 0, navBarHeight); } } \ No newline at end of file diff --git a/app/src/main/java/mw/gri/android/Utils.java b/app/src/main/java/mw/gri/android/Utils.java new file mode 100644 index 0000000..2725c9d --- /dev/null +++ b/app/src/main/java/mw/gri/android/Utils.java @@ -0,0 +1,75 @@ +package mw.gri.android; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Point; +import android.os.Build; +import android.view.Display; +import android.view.WindowInsets; +import android.view.WindowManager; + +public class Utils { + + public static int getStatusBarHeight(Context context) { + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) { + return windowManager + .getCurrentWindowMetrics() + .getWindowInsets() + .getInsets(WindowInsets.Type.navigationBars()) + .bottom; + } else { + Resources res = context.getResources(); + int statusBarHeight = 24; + @SuppressLint({"DiscouragedApi", "InternalInsetResource"}) + int resourceId = res.getIdentifier("status_bar_height", "dimen", "android"); + if (resourceId > 0) { + statusBarHeight = res.getDimensionPixelSize(resourceId); + } + return statusBarHeight; + } + } + + public static int getNavigationBarHeight(Context context) { + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) { + return windowManager + .getCurrentWindowMetrics() + .getWindowInsets() + .getInsets(WindowInsets.Type.navigationBars()) + .bottom; + } else { + Point appUsableSize = getAppUsableScreenSize(context); + Point realScreenSize = getRealScreenSize(context); + + // 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; + } + } + + private static Point getAppUsableScreenSize(Context context) { + WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + Point size = new Point(); + windowManager.getDefaultDisplay().getSize(size); + return size; + } + + private static 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; + } +} diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index f8c6127..554c178 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,10 +1,7 @@ - #FFBB86FC - #FF6200EE - #FF3700B3 - #FF03DAC5 - #FF018786 + #FFFEF102 + #FFFEF102 #FF000000 #FFFFFFFF \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index fa5fd2b..19b3ae1 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,16 +1,13 @@ - \ No newline at end of file