Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active November 3, 2019 17:10
Show Gist options
  • Save kristopherjohnson/6023531 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/6023531 to your computer and use it in GitHub Desktop.
Methods to hide or show the soft keyboard in an Android activity
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
/**
* Utility methods for manipulating the onscreen keyboard
*/
public class KeyboardUtil {
/**
* Hides the soft keyboard
*/
public static void hideSoftKeyboard(Activity activity) {
View focusedView = activity.getCurrentFocus();
if(focusedView != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
}
}
/**
* Shows the soft keyboard
*/
public static void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
// Class is not instantiable
private KeyboardUtil() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment