Skip to content

Instantly share code, notes, and snippets.

@juliomarcos
Last active August 29, 2015 14:05
Show Gist options
  • Save juliomarcos/eb42a6f84e275ff68635 to your computer and use it in GitHub Desktop.
Save juliomarcos/eb42a6f84e275ff68635 to your computer and use it in GitHub Desktop.
KeyboardUtils for Android
package /* YOUR PACKAGE HERE */;
import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class KeyboardUtils {
public interface OnDoneListener {
public void onDone();
}
public static void dismiss(Activity ctx) {
InputMethodManager inputManager = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);
//check if no view has focus:
View view = ctx.getCurrentFocus();
if (view == null)
return;
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
public static void setOnDoneListener(EditText editText, final OnDoneListener onDoneListener) {
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
onDoneListener.onDone();
return true;
}
return false;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment