Java Code Examples for android.app.Activity#getCurrentFocus()

The following examples show how to use android.app.Activity#getCurrentFocus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseFragment.java    From RxZhihuDaily with MIT License 6 votes vote down vote up
protected void showKeyboard() {
    Activity activity = getActivity();
    if (activity == null) {
        return;
    }

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

    if (activity.getCurrentFocus() == null) {
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    } else {
        imm.showSoftInput(activity.getCurrentFocus(), 0);
    }
}
 
Example 2
Source File: CountryPicker.java    From CountryPicker with Apache License 2.0 6 votes vote down vote up
/**
* Method to hide keyboard if it's open
*/
public void hideKeyboard() {
  try {
    Activity activity = getActivity();
    if (activity == null) {
      return;
    }
    InputMethodManager input
          = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    if (input == null) {
      return;
    }
    View currentFocus = activity.getCurrentFocus();
    if (currentFocus == null || currentFocus.getWindowToken() == null) {
      return;
    }

    input.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
  } catch (Exception e) {
      // ignoring any exceptions
  }
}
 
Example 3
Source File: BasicUiUtils.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * Hide soft keyboard method.
 *
 * @param context
 * @param activity
 */
public static void hiddenKeyboard(Context context, Activity activity) {
    try {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (activity.getCurrentFocus() != null) {
            if (activity.getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(activity
                                .getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example 4
Source File: BaseUtils.java    From orz with Apache License 2.0 6 votes vote down vote up
public static void showInputMethod(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);
            imm.showSoftInput(currentFocus, InputMethodManager.SHOW_IMPLICIT);
        }

//        if (imm != null) {
//            imm.viewClicked(this);
//        }
//        if (imm != null && getShowSoftInputOnFocus()) {
//            imm.showSoftInput(this, 0);
//        }
    }
 
Example 5
Source File: Utils.java    From KUAS-AP-Material with MIT License 5 votes vote down vote up
public static void hideSoftKeyboard(@NonNull Activity activity) {
	InputMethodManager inputManager =
			(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
	if (activity.getCurrentFocus() != null) {
		inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}
}
 
Example 6
Source File: UI.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private static void hideSoftKeyboard(final Activity activity) {
    if (activity == null)
        return;
    final InputMethodManager inputMethodManager =
        (InputMethodManager) activity.getSystemService(
            Activity.INPUT_METHOD_SERVICE);
    if (inputMethodManager == null || activity.getCurrentFocus() == null)
        return;
    inputMethodManager.hideSoftInputFromWindow(
        activity.getCurrentFocus().getWindowToken(), 0);
}
 
Example 7
Source File: KeyboardUtil.java    From Ruisi with Apache License 2.0 5 votes vote down vote up
public static void hideKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
 
Example 8
Source File: Utils.java    From Travel-Mate with MIT License 5 votes vote down vote up
public static void hideKeyboard(Activity th) {
    View view = th.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) th.getSystemService(Context.INPUT_METHOD_SERVICE);
        Objects.requireNonNull(inputManager)
                .hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
 
Example 9
Source File: HideUtil.java    From KUtils-master with Apache License 2.0 5 votes vote down vote up
/**
 * Forced hidden keyboard
 * @param activity
 */
public static void hideSoftKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    if (null != view) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
 
Example 10
Source File: MVViewModel.java    From GSYRickText with MIT License 5 votes vote down vote up
/**
 * 隐藏软键盘
 */
private void hideKeyboard() {
    Activity context = (Activity) imvvmView.getContext();
    if (context.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
        if (context.getCurrentFocus() != null) {
            ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).
                    hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
 
Example 11
Source File: ActivitySource.java    From Kalle with Apache License 2.0 5 votes vote down vote up
@Override
void closeInputMethod() {
    Activity activity = getSource();
    View focusView = activity.getCurrentFocus();
    if (focusView != null) {
        InputMethodManager manager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (manager != null) {
            manager.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
        }
    }
}
 
Example 12
Source File: KPSwitchConflictUtil.java    From imsdk-android with MIT License 5 votes vote down vote up
/**
 * Hide the panel and the keyboard.
 *
 * @param panelLayout the layout of panel.
 */
public static void hidePanelAndKeyboard(final View panelLayout) {
    final Activity activity = (Activity) panelLayout.getContext();

    final View focusView = activity.getCurrentFocus();
    if (focusView != null) {
        KeyboardUtil.hideKeyboard(activity.getCurrentFocus());
        focusView.clearFocus();
    }else{
        KeyboardUtil.hideKeyboard(panelLayout);
    }

    panelLayout.setVisibility(View.GONE);
}
 
Example 13
Source File: Utils.java    From KUAS-AP-Material with MIT License 5 votes vote down vote up
public static void hideSoftKeyboard(@NonNull Activity activity) {
	InputMethodManager inputManager =
			(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
	if (activity.getCurrentFocus() != null) {
		inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}
}
 
Example 14
Source File: Util.java    From VinylMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
public static void hideSoftKeyboard(@Nullable Activity activity) {
    if (activity != null) {
        View currentFocus = activity.getCurrentFocus();
        if (currentFocus != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
        }
    }
}
 
Example 15
Source File: DeviceUtils.java    From pandroid with Apache License 2.0 5 votes vote down vote up
public static void closeKeyboard(Activity activity) {
    if(activity == null)
        return;
    View view = activity.getCurrentFocus();
    if (view != null) {
        closeKeyboard(view);
    }
}
 
Example 16
Source File: Service.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Hides the onscreen keyboard if it is visisble
 */
public static void hideKeyboard(Activity activity) {
    // Check if no view has focus:
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
 
Example 17
Source File: AppHelper.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static void hideSoftInput(Activity activity) {
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
 
Example 18
Source File: AppHelper.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
public static void hideSoftInput(Activity activity) {
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
 
Example 19
Source File: InputMethodUtil.java    From SimpleProject with MIT License 5 votes vote down vote up
/**
 * 隐藏键盘
 * @param context
 */
public static void hideKeyboard(Activity context) {
	InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
	View view = context.getCurrentFocus();
	if (view != null && imm != null) {
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}
}
 
Example 20
Source File: SystemUIUtils.java    From Deadline with GNU General Public License v3.0 4 votes vote down vote up
public static void hideKeyBoard(Activity activity) {
    if ((activity.getCurrentFocus() != null) && (activity.getCurrentFocus().getWindowToken() != null)) {
        ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).
                hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }
}