Example usage for android.app Activity getCurrentFocus

List of usage examples for android.app Activity getCurrentFocus

Introduction

In this page you can find the example usage for android.app Activity getCurrentFocus.

Prototype

@Nullable
public View getCurrentFocus() 

Source Link

Document

Calls android.view.Window#getCurrentFocus on the Window of this Activity to return the currently focused view.

Usage

From source file:Main.java

public static void showKeyboard(Context context) {
    Activity activity = (Activity) context;
    if (activity != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }//from   w  w  w  .  ja  va 2s.com
}

From source file:Main.java

/**
 * Request to hide the soft input window from the context of the window that
 * is currently accepting input.//w w  w  .  ja va2s.  co  m
 * 
 * @param activity
 *            The currently activity, which shows the view receives soft
 *            keyboard input
 */
public static void showKeyboard(Activity activity) {
    if (null == activity) {
        return;
    }
    InputMethodManager im = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    View v = activity.getCurrentFocus();
    if (v != null) {
        im.showSoftInput(v, InputMethodManager.RESULT_UNCHANGED_SHOWN);
    }
}

From source file:Main.java

/**
 * Request to hide the soft input window from the context of the window that
 * is currently accepting input.//from  w w w  . ja  va  2  s. c  o  m
 * 
 * @param activity
 *            The currently activity, which shows the view receives soft
 *            keyboard input.
 */
public static void hideKeyboard(Activity activity) {
    if (null == activity) {
        return;
    }
    InputMethodManager im = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    View v = activity.getCurrentFocus();
    if (v != null) {
        im.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

From source file:Main.java

public static void dismissKeyBoard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isAcceptingText()) { // verify if the soft keyboard is open
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }/*  ww  w . j a va  2s .c  om*/
}

From source file:Main.java

public static void closeInputMethodWindow(Activity activity) {
    try {/*  w w  w .  j av  a 2s. c o m*/
        InputMethodManager inputMethodManager = (InputMethodManager) activity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    } catch (Throwable e) {
    }
}

From source file:Main.java

public static void dismissKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
    if (imm.isAcceptingText()) { // verify if the soft keyboard is open
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }//from   ww  w. jav a2 s  . c om
}

From source file:Main.java

/**
 * Chiude la tastiera virtuale./*from ww w.j ava  2  s  . c o  m*/
 * 
 * @param activity Attività.
 */
public static void hideSoftInput(Activity activity) {
    View view;
    InputMethodManager inputMgr;

    if (activity == null) {
        throw new NullPointerException("Argument activity is null.");
    }

    view = activity.getCurrentFocus();
    if (view == null) {
        return;
    }

    inputMgr = (InputMethodManager) view.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMgr.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

From source file:Main.java

public static void hideKeyboard(Activity activity) {
    if (activity != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }// w w  w.ja  v a 2 s  . com
    }
}

From source file:Main.java

public static void hideIME(Activity context, boolean bHide) {
    if (bHide) {//www . ja  v  a  2s. co m
        try {
            ((InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        }
    } else { // show IME
        try {
            ((InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE))
                    .showSoftInput(context.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        }
    }
}

From source file:Main.java

public static void showKeyboard(Activity activity) {
    if (activity != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (!imm.isActive()) {
            imm.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
        }/*from  w w  w  . j a v a2 s  . c o  m*/
    }
}