Skip to content

Instantly share code, notes, and snippets.

@vermilion1
Last active December 9, 2015 19:39
Show Gist options
  • Save vermilion1/4318799 to your computer and use it in GitHub Desktop.
Save vermilion1/4318799 to your computer and use it in GitHub Desktop.
Cordova Android Keyboard Plugin
<plugin name="KeyBoard" value="com.tamtoto.KeyBoardPlugin" />
toggleKeyboard : function (show) {
cordova.exec(null, null, 'KeyBoard', show ? 'show' : 'hide', []);
}
package com.LALALA;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
public class KeyBoardPlugin extends CordovaPlugin {
public void showKeyBoard() {
try {
InputMethodManager mgr = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(webView, InputMethodManager.SHOW_IMPLICIT);
((InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(webView, 0);
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
webView.dispatchKeyEvent(new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MOVE_END ));
webView.dispatchKeyEvent(new KeyEvent( KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MOVE_END ));
}
});
}
catch(Exception e) {}
}
public void hideKeyBoard() {
InputMethodManager mgr = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(webView.getWindowToken(), 0);
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("show")) {
this.showKeyBoard();
callbackContext.success("done");
return true;
}
else if (action.equals("hide")) {
this.hideKeyBoard();
callbackContext.success("done");
return true;
}
callbackContext.error("INVALID_ACTION");
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment