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

Commit cff381d

Browse files
committed
Added Keyboard.show function for Android. This can be used to force the soft keyboard to appear if an auto focus on a form text element does not.
1 parent 3dd0060 commit cff381d

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/android/IonicKeyboard.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ionic.keyboard;
1+
package com.ionic.keyboard;
22

33
import org.apache.cordova.CallbackContext;
44
import org.apache.cordova.CordovaInterface;
@@ -19,33 +19,33 @@ public class IonicKeyboard extends CordovaPlugin{
1919

2020
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
2121
super.initialize(cordova, webView);
22-
22+
2323
//calculate density-independent pixels (dp)
2424
//http://developer.android.com/guide/practices/screens_support.html
2525
DisplayMetrics dm = new DisplayMetrics();
2626
cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
2727
final float density = dm.density;
28-
28+
2929
final CordovaWebView appView = webView;
30-
30+
3131
//http://stackoverflow.com/a/4737265/1091751 detect if keyboard is showing
3232
final View rootView = cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView();
3333
OnGlobalLayoutListener list = new OnGlobalLayoutListener() {
3434
int previousHeightDiff = 0;
3535
@Override
3636
public void onGlobalLayout() {
37-
Rect r = new Rect();
37+
Rect r = new Rect();
3838
//r will be populated with the coordinates of your view that area still visible.
3939
rootView.getWindowVisibleDisplayFrame(r);
4040

4141
int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);
4242
int pixelHeightDiff = (int)(heightDiff / density);
4343
if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
4444
appView.sendJavascript("cordova.plugins.Keyboard.isVisible = true");
45-
appView.sendJavascript("cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});");
45+
appView.sendJavascript("cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});");
4646

4747
//deprecated
48-
appView.sendJavascript("cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});");
48+
appView.sendJavascript("cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});");
4949
}
5050
else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){
5151
appView.sendJavascript("cordova.plugins.Keyboard.isVisible = false");
@@ -56,31 +56,40 @@ else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelH
5656
}
5757
previousHeightDiff = pixelHeightDiff;
5858
}
59-
};
60-
59+
};
60+
6161
rootView.getViewTreeObserver().addOnGlobalLayoutListener(list);
6262
}
63-
63+
6464
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
6565
if ("close".equals(action)) {
6666
cordova.getThreadPool().execute(new Runnable() {
6767
public void run() {
68-
//http://stackoverflow.com/a/7696791/1091751
69-
InputMethodManager inputManager = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
70-
View v = cordova.getActivity().getCurrentFocus();
71-
72-
if (v == null) {
73-
callbackContext.error("No current focus");
74-
}
75-
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
68+
//http://stackoverflow.com/a/7696791/1091751
69+
InputMethodManager inputManager = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
70+
View v = cordova.getActivity().getCurrentFocus();
71+
72+
if (v == null) {
73+
callbackContext.error("No current focus");
74+
}
75+
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
76+
callbackContext.success(); // Thread-safe.
77+
}
78+
});
79+
return true;
80+
}
81+
if ("show".equals(action)) {
82+
cordova.getThreadPool().execute(new Runnable() {
83+
public void run() {
84+
((InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
7685
callbackContext.success(); // Thread-safe.
7786
}
7887
});
7988
return true;
8089
}
8190
return false; // Returning false results in a "MethodNotFound" error.
8291
}
83-
92+
8493

8594
}
8695

www/keyboard.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var argscheck = require('cordova/argscheck'),
33
utils = require('cordova/utils'),
44
exec = require('cordova/exec');
5-
5+
66
var Keyboard = function() {
77
};
88

@@ -14,6 +14,10 @@ Keyboard.close = function() {
1414
exec(null, null, "Keyboard", "close", []);
1515
};
1616

17+
Keyboard.show = function() {
18+
exec(null, null, "Keyboard", "show", []);
19+
};
20+
1721
Keyboard.disableScroll = function(disable) {
1822
exec(null, null, "Keyboard", "disableScroll", [disable]);
1923
};
@@ -24,7 +28,7 @@ Keyboard.styleDark = function(dark) {
2428
};
2529
*/
2630

27-
Keyboard.isVisible = false;
31+
Keyboard.isVisible = false;
2832

2933
module.exports = Keyboard;
3034

0 commit comments

Comments
 (0)