16
16
import android .view .ViewTreeObserver .OnGlobalLayoutListener ;
17
17
import android .view .inputmethod .InputMethodManager ;
18
18
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
+
19
24
public class IonicKeyboard extends CordovaPlugin {
20
25
21
26
public void initialize (CordovaInterface cordova , CordovaWebView webView ) {
@@ -67,13 +72,31 @@ public void onGlobalLayout() {
67
72
Rect r = new Rect ();
68
73
//r will be populated with the coordinates of your view that area still visible.
69
74
rootView .getWindowVisibleDisplayFrame (r );
70
-
75
+
71
76
PluginResult result ;
72
77
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
+
74
97
int pixelHeightDiff = (int )(heightDiff / density );
75
98
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 );
77
100
result = new PluginResult (PluginResult .Status .OK , msg );
78
101
result .setKeepCallback (true );
79
102
callbackContext .sendPluginResult (result );
@@ -89,8 +112,8 @@ else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelH
89
112
};
90
113
91
114
rootView .getViewTreeObserver ().addOnGlobalLayoutListener (list );
92
-
93
-
115
+
116
+
94
117
PluginResult dataResult = new PluginResult (PluginResult .Status .OK );
95
118
dataResult .setKeepCallback (true );
96
119
callbackContext .sendPluginResult (dataResult );
0 commit comments