Skip to content
This repository was archived by the owner on Apr 2, 2018. It is now read-only.

Commit 7a41e62

Browse files
SebastianSchirmertlancina
authored andcommitted
Fix Android screen height calculation (#203)
* Fix Android screen height calculation * Remove status bar height from being calculated and passed as additional result * Remove "int resultTop = r.top;" as not used anywhere
1 parent 055d03f commit 7a41e62

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/android/IonicKeyboard.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
1717
import android.view.inputmethod.InputMethodManager;
1818

19+
// import additionally required classes for calculating screen height
20+
import android.view.Display;
21+
import android.graphics.Point;
22+
import android.os.Build;
23+
1924
public class IonicKeyboard extends CordovaPlugin {
2025

2126
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
@@ -67,13 +72,31 @@ public void onGlobalLayout() {
6772
Rect r = new Rect();
6873
//r will be populated with the coordinates of your view that area still visible.
6974
rootView.getWindowVisibleDisplayFrame(r);
70-
75+
7176
PluginResult result;
7277

73-
int heightDiff = rootView.getRootView().getHeight() - r.bottom;
78+
// cache properties for later use
79+
int rootViewHeight = rootView.getRootView().getHeight();
80+
int resultBottom = r.bottom;
81+
82+
// calculate screen height differently for android versions >= 21: Lollipop 5.x, Marshmallow 6.x
83+
//http://stackoverflow.com/a/29257533/3642890 beware of nexus 5
84+
int screenHeight;
85+
86+
if (Build.VERSION.SDK_INT >= 21) {
87+
Display display = cordova.getActivity().getWindowManager().getDefaultDisplay();
88+
Point size = new Point();
89+
display.getSize(size);
90+
screenHeight = size.y;
91+
} else {
92+
screenHeight = rootViewHeight;
93+
}
94+
95+
int heightDiff = screenHeight - resultBottom;
96+
7497
int pixelHeightDiff = (int)(heightDiff / density);
7598
if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
76-
String msg = "S" + Integer.toString(pixelHeightDiff);
99+
String msg = "S" + Integer.toString(pixelHeightDiff);
77100
result = new PluginResult(PluginResult.Status.OK, msg);
78101
result.setKeepCallback(true);
79102
callbackContext.sendPluginResult(result);
@@ -89,8 +112,8 @@ else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelH
89112
};
90113

91114
rootView.getViewTreeObserver().addOnGlobalLayoutListener(list);
92-
93-
115+
116+
94117
PluginResult dataResult = new PluginResult(PluginResult.Status.OK);
95118
dataResult.setKeepCallback(true);
96119
callbackContext.sendPluginResult(dataResult);

www/android/keyboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Keyboard.hideKeyboardAccessoryBar = function(hide) {
1212
exec(null, null, "Keyboard", "hideKeyboardAccessoryBar", [hide]);
1313
};
1414

15-
Keyboard.close = function() {
15+
Keyboard.close = function() {
1616
exec(null, null, "Keyboard", "close", []);
1717
};
1818

0 commit comments

Comments
 (0)