Last active
November 3, 2019 17:10
-
-
Save kristopherjohnson/6023531 to your computer and use it in GitHub Desktop.
Methods to hide or show the soft keyboard in an Android activity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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