Hide any keyboard that might be open - Android android.view.inputmethod

Android examples for android.view.inputmethod:InputMethodManager

Description

Hide any keyboard that might be open

Demo Code

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class Main {

  /**/*  w w w .  j av  a 2  s  .  co m*/
   * Hide any keyboard that might be open
   */
  public static void hideKeyboard(Activity activity) {
    InputMethodManager inputMethodService = (InputMethodManager) activity
        .getSystemService(Context.INPUT_METHOD_SERVICE);
    // if (inputMethodService != null && inputMethodService.isActive()) {
    if (inputMethodService != null) {
      inputMethodService.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
    }
  }

  public static void hideKeyboard(Activity activity, View view) {
    InputMethodManager inputMethodService = (InputMethodManager) activity
        .getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputMethodService != null && inputMethodService.isActive()) {
      inputMethodService.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
  }

}

Related Tutorials