Commit db2bc8ac authored by Jack Hsu's avatar Jack Hsu

updates code and README.md

parent 8fefd38d
...@@ -22,6 +22,7 @@ const Example = React.createClass({ ...@@ -22,6 +22,7 @@ const Example = React.createClass({
</View> </View>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>REAL_WINDOW_HEIGHT ({ExtraDimensions.get('REAL_WINDOW_HEIGHT')})</Text> <Text>REAL_WINDOW_HEIGHT ({ExtraDimensions.get('REAL_WINDOW_HEIGHT')})</Text>
<Text>REAL_WINDOW_WIDTH ({ExtraDimensions.get('REAL_WINDOW_WIDTH')})</Text>
</View> </View>
<View style={{ backgroundColor: 'blue', justifyContent: 'center', alignItems: 'center', height: ExtraDimensions.get('SOFT_MENU_BAR_HEIGHT')}}> <View style={{ backgroundColor: 'blue', justifyContent: 'center', alignItems: 'center', height: ExtraDimensions.get('SOFT_MENU_BAR_HEIGHT')}}>
<Text style={{ color: 'white' }}>SOFT_MENU_BAR_HEIGHT ({ExtraDimensions.get('SOFT_MENU_BAR_HEIGHT')})</Text> <Text style={{ color: 'white' }}>SOFT_MENU_BAR_HEIGHT ({ExtraDimensions.get('SOFT_MENU_BAR_HEIGHT')})</Text>
......
## ExtraDimensions ## ExtraDimensions
This module allows you to access additional display metrics. This module allows you to access additional display metrics on Android devices.
- Actual width and height of the screen (including elements such as soft menu bar)
- Soft menu height
- Status bar height
### Installation
### Demo
![](./demo.png)
### API
There is only one method `get(dimension: string)` that takes in a dimension name, and returns its value as a `number`.
Supported dimensions are:
- `REAL_WINDOW_HEIGHT` - Actual height of screen including system decor elements
- `REAL_WINDOW_WIDTH` - Actual width of screen including system decor elements
- `STATUS_BAR_HEIGHT` - Height of the status bar
- `SOFT_MENU_BAR_HEIGHT` - Height of the soft menu bar (supported on most new Android devices)
package ca.jaysoo.extradimensions; package ca.jaysoo.extradimensions;
import java.lang.Math; import java.lang.Math;
import java.lang.reflect.InvocationTargetException;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.app.Activity; import android.view.Display;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
...@@ -28,41 +30,59 @@ public class ExtraDimensionsModule extends ReactContextBaseJavaModule { ...@@ -28,41 +30,59 @@ public class ExtraDimensionsModule extends ReactContextBaseJavaModule {
@Override @Override
public Map<String, Object> getConstants() { public Map<String, Object> getConstants() {
Map<String, Object> constants = new HashMap<>(); final Map<String, Object> constants = new HashMap<>();
constants.put("STATUS_BAR_HEIGHT", getStatusBarHeight()); final Context ctx = getReactApplicationContext();
constants.put("SOFT_MENU_BAR_HEIGHT", getSoftMenuBarHeight()); final DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
constants.put("REAL_WINDOW_HEIGHT", getRealHeight());
// Get the real display metrics if we are using API level 17 or higher.
// The real metrics include system decor elements (e.g. soft menu bar).
//
// See: http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics)
if (Build.VERSION.SDK_INT >= 17) {
Display display = mCurrentActivity.getWindowManager().getDefaultDisplay();
try {
Display.class.getMethod("getRealMetrics", DisplayMetrics.class).invoke(display, metrics);
} catch (InvocationTargetException e) {
} catch (IllegalAccessException e) {
} catch (NoSuchMethodException e) {
}
}
constants.put("REAL_WINDOW_HEIGHT", getRealHeight(metrics));
constants.put("REAL_WINDOW_WIDTH", getRealWidth(metrics));
constants.put("STATUS_BAR_HEIGHT", getStatusBarHeight(metrics));
constants.put("SOFT_MENU_BAR_HEIGHT", getSoftMenuBarHeight(metrics));
return constants; return constants;
} }
private float getStatusBarHeight() { private float getStatusBarHeight(DisplayMetrics metrics) {
Context ctx = getReactApplicationContext(); final Context ctx = getReactApplicationContext();
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics(); final int heightResId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
int heightResId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
return return
heightResId > 0 heightResId > 0
? ctx.getResources().getDimensionPixelSize(heightResId) / metrics.density ? ctx.getResources().getDimensionPixelSize(heightResId) / metrics.density
: 0; : 0;
} }
private float getSoftMenuBarHeight() { private float getSoftMenuBarHeight(DisplayMetrics metrics) {
Context ctx = getReactApplicationContext(); final float realHeight = getRealHeight(metrics);
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics(); final Context ctx = getReactApplicationContext();
final DisplayMetrics usableMetrics = ctx.getResources().getDisplayMetrics();
mCurrentActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics); mCurrentActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels; final int usableHeight = usableMetrics.heightPixels;
return Math.max(0, getRealHeight() - usableHeight / metrics.density); return Math.max(0, realHeight - usableHeight / metrics.density);
} }
private float getRealHeight() { private float getRealHeight(DisplayMetrics metrics) {
Context ctx = getReactApplicationContext();
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
mCurrentActivity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
return metrics.heightPixels / metrics.density; return metrics.heightPixels / metrics.density;
} }
private float getRealWidth(DisplayMetrics metrics) {
return metrics.widthPixels / metrics.density;
}
} }
{ {
"name": "react-native-extra-dimensions-android", "name": "react-native-extra-dimensions-android",
"version": "0.17.0", "version": "0.17.0",
"description": "", "description": "Access additional display metrics on Android devices: status bar height, soft menu bar height, real screen size.",
"main": "index.js", "main": "index.js",
"repository": { "repository": {
"type": "git", "type": "git",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment