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

Commit 6636fea

Browse files
committed
Merge pull request #25 from basvanbeek/master
Addition of Keyboard.show function for Android
2 parents 3dd0060 + d672505 commit 6636fea

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Methods
1111
- cordova.plugins.Keyboard.hideKeyboardAccessoryBar
1212
- cordova.plugins.Keyboard.close
1313
- cordova.plugins.Keyboard.disableScroll
14+
- cordova.plugins.Keyboard.show
1415

1516
Properties
1617
--------
@@ -20,7 +21,7 @@ Properties
2021
Events
2122
--------
2223

23-
These events are fired on the window.
24+
These events are fired on the window.
2425

2526
- native.keyboardshow
2627
* A number `keyboardHeight` is given on the event object, which is the pixel height of the keyboard.
@@ -62,7 +63,7 @@ Supported Platforms
6263

6364
- iOS, Android
6465

65-
66+
6667
Keyboard.disableScroll
6768
=================
6869

@@ -76,22 +77,32 @@ Supported Platforms
7677

7778
- iOS
7879

80+
Keyboard.show
81+
=================
82+
83+
Force keyboard to be shown on Android. This typically helps if autofocus on a text element does not pop up the keyboard automatically
84+
85+
cordova.plugins.Keyboard.show();
86+
87+
Supported Platforms
88+
89+
- Android
7990

8091
native.keyboardshow
8192
=================
8293

8394
This event fires when the keyboard will be shown
8495

8596
window.addEventListener('native.keyboardshow', keyboardShowHandler);
86-
97+
8798
function keyboardShowHandler(e){
8899
alert('Keyboard height is: ' + e.keyboardHeight);
89100
}
90101

91102
Properties
92103
-----------
93104

94-
keyboardHeight: the height of the keyboard in pixels
105+
keyboardHeight: the height of the keyboard in pixels
95106

96107

97108
Supported Platforms
@@ -106,7 +117,7 @@ native.keyboardhide
106117
This event fires when the keyboard will hide
107118

108119
window.addEventListener('native.keyboardhide', keyboardHideHandler);
109-
120+
110121
function keyboardHideHandler(e){
111122
alert('Goodnight, sweet prince');
112123
}

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)