Example usage for android.content Context INPUT_METHOD_SERVICE

List of usage examples for android.content Context INPUT_METHOD_SERVICE

Introduction

In this page you can find the example usage for android.content Context INPUT_METHOD_SERVICE.

Prototype

String INPUT_METHOD_SERVICE

To view the source code for android.content Context INPUT_METHOD_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.view.inputmethod.InputMethodManager for accessing input methods.

Usage

From source file:Main.java

/**
 * Hide input method.//  w w w .j a  v a 2 s  .com
 *
 * @param activity
 */
public static void hideInputMethod(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    View currentFocus = activity.getCurrentFocus();
    if (currentFocus != null) {
        imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

From source file:Main.java

public static void hideKeyboard(View view) {
    if (view == null) {
        return;/*from  w  ww  . ja v  a 2s .c om*/
    }
    try {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (!imm.isActive()) {
            return;
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * This method is used to hide a keyboard after a user has
 * finished typing the input.//from w ww .  j  a va 2s . co m
 */
public static void hideKeyboard(Activity activity, IBinder windowToken) {
    InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(windowToken, 0);
}

From source file:Main.java

public static void showKeyboard(View view) {
    if (view == null) {
        return;/*from  w w w  .ja  v a 2 s  .  c  om*/
    }
    try {
        InputMethodManager inputManager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Hides the keyboard while switching among fragments.
 * /*from  ww  w.  j  a v  a 2  s .c  o  m*/
 * @param context of activity.
 */
public static void hideKeyboard(Context context) {
    try {
        InputMethodManager inputManager = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(), 0);
    } catch (Exception ex) {
    }
}

From source file:Main.java

public static void fixInputMethodManagerLeak(Context context) {
    if (context == null) {
        return;/*  ww w.j  a va2s. com*/
    }

    try {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }

        Field f_mCurRootView = imm.getClass().getDeclaredField("mCurRootView");
        Field f_mServedView = imm.getClass().getDeclaredField("mServedView");
        Field f_mNextServedView = imm.getClass().getDeclaredField("mNextServedView");

        f_mCurRootView.setAccessible(true);
        f_mCurRootView.set(imm, null);

        f_mServedView.setAccessible(true);
        f_mServedView.set(imm, null);

        f_mNextServedView.setAccessible(true);
        f_mNextServedView.set(imm, null);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

From source file:Main.java

public static void hideSoftKeyBoard(Context context, final View... views) {
    try {/* w  ww  . java2 s .c  o m*/
        final InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    if (views != null) {
                        for (View view : views) {
                            if (view != null) {
                                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                            }
                        }
                    }
                } catch (Exception e) {
                }
            }
        }, 200);
    } catch (Exception e) {
    }
}

From source file:Main.java

public static boolean isKeyboardShowed(View view) {
    if (view == null) {
        return false;
    }/*ww w  .  ja  v  a2  s.co m*/
    try {
        InputMethodManager inputManager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        return inputManager.isActive(view);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

/**
 * This method hides the keyboard/*from  ww w  .j ava2  s . co m*/
 *
 * @param activity
 *         Active activity
 */
public static void hideKeyboard(Activity activity) {
    InputMethodManager inputManager = (InputMethodManager) activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    View view = activity.getCurrentFocus();
    if (view != null) {
        inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

From source file:Main.java

/**
 * Hide keyboard//from   ww w.j a v a 2  s.  c  o  m
 * 
 * @param act
 *            Activity
 * @param view
 */
public static void hideKeyboard(Activity act, View view) {
    InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}