Skip to content

Instantly share code, notes, and snippets.

@liminal
Last active April 3, 2018 09:11
Show Gist options
  • Save liminal/5a4bb1fce48efa38e41d to your computer and use it in GitHub Desktop.
Save liminal/5a4bb1fce48efa38e41d to your computer and use it in GitHub Desktop.
Utility for hiding android keyboard #android
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
public class KeyboardUtil {
public static void hideKeyboard(final Context ctx, final View view) {
InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
if (view != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} else {
if (ctx instanceof Activity) {
((Activity)ctx).getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment