Created
September 7, 2015 09:09
-
-
Save plastiv/4590ad5fa50de24b5ce5 to your computer and use it in GitHub Desktop.
show or hide keyboard manually from code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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