Skip to content

Instantly share code, notes, and snippets.

@Bresiu
Created December 8, 2017 14:16
Show Gist options
  • Save Bresiu/fa3dc05446c262eeb3920671167e6e6e to your computer and use it in GitHub Desktop.
Save Bresiu/fa3dc05446c262eeb3920671167e6e6e to your computer and use it in GitHub Desktop.
package com.instantag.instantags.utils;
import android.app.Activity;
import android.content.Context;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
public class Keyboard {
public static void enableInputMode(@NonNull Context context, @NonNull View viewToGainFocus) {
viewToGainFocus.requestFocus();
getInputMethodManager(context).showSoftInput(viewToGainFocus, InputMethodManager.SHOW_FORCED);
}
public static void disableInputMode(@NonNull Activity activity, @NonNull View viewToLostFocus) {
viewToLostFocus.clearFocus();
getInputMethodManager(activity).hideSoftInputFromWindow(getIBinder(activity), 0);
}
private static InputMethodManager getInputMethodManager(@NonNull Context context) {
return (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
}
private static IBinder getIBinder(@NonNull Activity activity) {
View view = activity.getCurrentFocus();
if (view == null) {
view = new View(activity);
}
return view.getWindowToken();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment