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({
</View>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>REAL_WINDOW_HEIGHT ({ExtraDimensions.get('REAL_WINDOW_HEIGHT')})</Text>
<Text>REAL_WINDOW_WIDTH ({ExtraDimensions.get('REAL_WINDOW_WIDTH')})</Text>
</View>
<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>
......
## 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;
import java.lang.Math;
import java.lang.reflect.InvocationTargetException;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.app.Activity;
import android.view.Display;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
......@@ -28,41 +30,59 @@ public class ExtraDimensionsModule extends ReactContextBaseJavaModule {
@Override
public Map<String, Object> getConstants() {
Map<String, Object> constants = new HashMap<>();
constants.put("STATUS_BAR_HEIGHT", getStatusBarHeight());
constants.put("SOFT_MENU_BAR_HEIGHT", getSoftMenuBarHeight());
constants.put("REAL_WINDOW_HEIGHT", getRealHeight());
final Map<String, Object> constants = new HashMap<>();
final Context ctx = getReactApplicationContext();
final DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
// 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;
}
private float getStatusBarHeight() {
Context ctx = getReactApplicationContext();
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
int heightResId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
private float getStatusBarHeight(DisplayMetrics metrics) {
final Context ctx = getReactApplicationContext();
final int heightResId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
return
heightResId > 0
? ctx.getResources().getDimensionPixelSize(heightResId) / metrics.density
: 0;
}
private float getSoftMenuBarHeight() {
Context ctx = getReactApplicationContext();
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
private float getSoftMenuBarHeight(DisplayMetrics metrics) {
final float realHeight = getRealHeight(metrics);
final Context ctx = getReactApplicationContext();
final DisplayMetrics usableMetrics = ctx.getResources().getDisplayMetrics();
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() {
Context ctx = getReactApplicationContext();
DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
mCurrentActivity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
private float getRealHeight(DisplayMetrics metrics) {
return metrics.heightPixels / metrics.density;
}
private float getRealWidth(DisplayMetrics metrics) {
return metrics.widthPixels / metrics.density;
}
}
{
"name": "react-native-extra-dimensions-android",
"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",
"repository": {
"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