Skip to content

Commit

Permalink
add background nav color setter
Browse files Browse the repository at this point in the history
  • Loading branch information
wanderbon committed Jul 26, 2021
1 parent f5bbd60 commit 8a6c733
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store

# Built application files
*.apk
*.ap_
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@

package com.reactlibrary;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.ViewConfiguration;
import android.view.WindowManager;

import androidx.annotation.RequiresApi;

import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -49,6 +54,7 @@ public void onHostResume() {

}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
Expand Down Expand Up @@ -79,6 +85,16 @@ public Map<String, Object> getConstants() {
return constants;
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@ReactMethod
public void setNavBackgroundColor(String color) {
if (color != null) {
int newColor = Color.parseColor(color);
((Activity) mReactContext.getApplicationContext()).getWindow().setNavigationBarColor(newColor);
}
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
private boolean hasPermanentMenuKey() {
final Context ctx = getReactApplicationContext();

Expand Down
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { NativeModules, Platform } from 'react-native';

const DectectModule = NativeModules.RNDetectSoftwareNavigationBar;

export function get(dim) {
if (Platform.OS !== 'android') {
return 0;
} else {
try {
if (!NativeModules.RNDetectSoftwareNavigationBar) {
if (!DectectModule) {
throw "RNDetectSoftwareNavigationBar not defined. Try rebuilding your project. e.g. react-native run-android";
}
const result = NativeModules.RNDetectSoftwareNavigationBar[dim];
const result = DectectModule[dim];

if (typeof result !== 'number') {
return result;
Expand Down Expand Up @@ -48,6 +50,14 @@ export function isEmulator() {
return get('IS_EMULATOR');
}

export function setNavBackgroundColor(color = '#ffffff') {
if (!DectectModule) {
throw "RNDetectSoftwareNavigationBar not defined. Try rebuilding your project. e.g. react-native run-android";
}

DectectModule.setNavBackgroundColor(color);
}

// stay compatible with pre-es6 exports
export default {
get,
Expand Down

0 comments on commit 8a6c733

Please sign in to comment.