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

Commit ff4ef18

Browse files
committed
add Android stuff
1 parent 838c71c commit ff4ef18

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

plugin.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
<clobbers target="window.Keyboard" />
1717
</js-module>
1818

19+
<!-- android -->
20+
<platform name="android">
21+
22+
<config-file target="config.xml" parent="/*">
23+
<feature name="Keyboard">
24+
<param name="android-package" value="com.ionic.keyboard.IonicKeyboard" />
25+
<param name="onload" value="true" />
26+
</feature>
27+
</config-file>
28+
29+
<source-file src="src/android/IonicKeyboard.java" target-dir="src/com/ionic/keyboard" />
30+
</platform>
31+
1932
<!-- ios -->
2033
<platform name="ios">
2134
<config-file target="config.xml" parent="/*">

src/android/IonicKeyboard.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.ionic.keyboard;
2+
3+
import org.apache.cordova.CordovaInterface;
4+
import org.apache.cordova.CordovaPlugin;
5+
import org.apache.cordova.CordovaWebView;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
9+
import android.graphics.Rect;
10+
import android.util.DisplayMetrics;
11+
import android.view.View;
12+
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
13+
14+
public class IonicKeyboard extends CordovaPlugin{
15+
16+
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
17+
super.initialize(cordova, webView);
18+
19+
//calculate density-independent pixels (dp)
20+
//http://developer.android.com/guide/practices/screens_support.html
21+
DisplayMetrics dm = new DisplayMetrics();
22+
cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
23+
final int density = (int)(dm.density);
24+
25+
final CordovaWebView appView = webView;
26+
27+
final View rootView = cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView();
28+
OnGlobalLayoutListener list = new OnGlobalLayoutListener() {
29+
int previousHeightDiff = 0;
30+
@Override
31+
public void onGlobalLayout() {
32+
Rect r = new Rect();
33+
//r will be populated with the coordinates of your view that area still visible.
34+
rootView.getWindowVisibleDisplayFrame(r);
35+
36+
int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);
37+
if (heightDiff > 200 && heightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
38+
int keyboardHeight = (int)(heightDiff / density);
39+
appView.sendJavascript("cordova.fireWindowEvent('showkeyboard', { 'keyboardHeight':" + Integer.toString(keyboardHeight)+"});");
40+
}
41+
previousHeightDiff = heightDiff;
42+
}
43+
};
44+
45+
rootView.getViewTreeObserver().addOnGlobalLayoutListener(list);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)