Skip to content

Instantly share code, notes, and snippets.

@kioltk
Created October 23, 2014 01:34
Show Gist options
  • Save kioltk/8f611a3ee4d1f7729b87 to your computer and use it in GitHub Desktop.
Save kioltk/8f611a3ee4d1f7729b87 to your computer and use it in GitHub Desktop.
Hide keyboard easily
package com.droidkit.util
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
/**
* Created by kiolt_000 on 23/10/2014.
*/
public class KeyboardUtil {
public static void hide(View probablyFocusedView, Activity activity) {
if (probablyFocusedView != null)
probablyFocusedView.clearFocus();
if (activity != null) {
activity.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
View focusedView = activity.getCurrentFocus();
if (focusedView != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment