Closing or hiding the Android soft keyboard programmatically can enhance user experience by managing the keyboard’s visibility based on the application’s context. This can be done using the InputMethodManager
class in Android. By obtaining an instance of InputMethodManager
and calling its hideSoftInputFromWindow
method, you can programmatically control the keyboard visibility. This method requires a reference to the current view window token, which can be retrieved from the view that is currently focused.
Using InputMethodManager
Basic Implementation:
To hide the keyboard, use the InputMethodManager
:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
getSystemService(Context.INPUT_METHOD_SERVICE)
: Retrieves the InputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0)
: Hides the keyboard.
Example:
public void hideKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
- Call this method and pass the current focused view.
Using Activity Context
Hide Keyboard in Activity:
To hide the keyboard in an activity context:
public void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
activity.getCurrentFocus()
: Retrieves the current focused view in the activity.
Usage:
hideKeyboard(this);
- Call
hideKeyboard
method in your activity.
Using Fragment Context
Hide Keyboard in Fragment:
To hide the keyboard in a fragment context:
public void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
View view = getActivity().getCurrentFocus();
if (view == null) {
view = new View(getActivity());
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
getActivity()
: Retrieves the activity associated with the fragment.
Usage:
hideKeyboard();
- Call
hideKeyboard
method in your fragment.
Using Utility Method
Utility Method:
Create a utility method to hide the keyboard:
public class KeyboardUtils {
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
- This utility method can be reused across different activities and fragments.
Usage:
KeyboardUtils.hideKeyboard(this);
- Call
hideKeyboard
method in your activity or fragment.
Automatic Keyboard Hiding
On Touch Outside:
Hide the keyboard when touching outside an input field:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View view = getCurrentFocus();
if (view instanceof EditText) {
Rect outRect = new Rect();
view.getGlobalVisibleRect(outRect);
if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
view.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(event);
}
- This hides the keyboard when tapping outside an
EditText
.
Using Window Flags
Window Flags:
Disable the soft keyboard by setting window flags:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
- This prevents the keyboard from automatically appearing when the activity starts.
In Manifest:
Set windowSoftInputMode in the manifest:
- This configuration hides the keyboard when the activity is launched.
Summary
Hiding the Android soft keyboard programmatically enhances user experience and can be achieved through various methods using InputMethodManager
. Implementing these methods allows for efficient control over the keyboard’s visibility, whether in activities, fragments, or using utility methods. Additionally, automatic hiding mechanisms, such as detecting touches outside input fields or setting window flags, provide seamless integration into user interactions. By leveraging these techniques, developers can create more polished and user-friendly applications.