Skip to content

Instantly share code, notes, and snippets.

@TheFinestArtist
Created July 25, 2015 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheFinestArtist/fcbe646bd373799a05fb to your computer and use it in GitHub Desktop.
Save TheFinestArtist/fcbe646bd373799a05fb to your computer and use it in GitHub Desktop.
KeyboardHelper.java
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
/**
* KeyboardHelper
* It pops up keyboard to input texts and drops a cursor to a view(final View view, such as Edittext)
*
* @author The Finest Artist
*/
public class KeyboardHelper {
public static void show(final Context context, final View view) {
if (view == null)
return;
view.requestFocus();
view.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(view, 0);
}
}, 200);
}
public static void showByUser(Context context, View view) {
if (view == null)
return;
InputMethodManager keyboard = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
public static void hide(Context context, View view) {
if (view == null)
return;
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static void hideByUser(Context context, View view) {
if (view == null)
return;
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment