Skip to content

Instantly share code, notes, and snippets.

@plastiv
Created September 7, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plastiv/4590ad5fa50de24b5ce5 to your computer and use it in GitHub Desktop.
Save plastiv/4590ad5fa50de24b5ce5 to your computer and use it in GitHub Desktop.
show or hide keyboard manually from code
import android.app.Activity;
import android.content.Context;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import timber.log.Timber;
public class InputManager {
private InputManager() {
}
public static void showSoftKeyboard(@NonNull Activity activity, @NonNull View view) {
if (view.requestFocus()) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
public static void hideSoftKeyboard(@NonNull Activity activity, @Nullable View view) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder windowToken;
if (view != null) {
windowToken = view.getWindowToken();
imm.hideSoftInputFromWindow(windowToken, 0);
}else if (activity.getCurrentFocus() != null) {
windowToken = activity.getCurrentFocus().getWindowToken();
imm.hideSoftInputFromWindow(windowToken, 0);
} else {
Timber.w("Soft keyboard was not hided");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment