1616import android .view .ViewTreeObserver .OnGlobalLayoutListener ;
1717import 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+
1924public 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 );
0 commit comments