Created
July 25, 2015 14:11
-
-
Save TheFinestArtist/fcbe646bd373799a05fb to your computer and use it in GitHub Desktop.
KeyboardHelper.java
This file contains hidden or 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.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