Related
I want to automatically show the soft-keyboard when an EditText is focused (if the device does not have a physical keyboard) and I have two problems:
When my Activity is displayed, my EditText is focused but the keyboard is not displayed, I need to click again on it to show the keyboard (it should be displayed when my Activity is displayed).
And when I click done on the keyboard, the keyboard is dissmissed but the EditText stays focused and y don't want (because my edit is done).
To resume, my problem is to have something more like on the iPhone: which keep the keyboard sync with my EditText state (focused / not focused) and of course does not present a soft-keyboard if there is a physical one.
To force the soft keyboard to appear, you can use
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
And for removing the focus on EditText, sadly you need to have a dummy View to grab focus.
To close it you can use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
This works for using it in a dialog
public void showKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public void closeKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
I had the same problem. Immediately after editText VISIBILITY change from GONE to VISIBLE, I had to set the focus and display the soft keyboard. I achieved this using the following code:
new Handler().postDelayed(new Runnable() {
public void run() {
// ((EditText) findViewById(R.id.et_find)).requestFocus();
//
EditText yourEditText= (EditText) findViewById(R.id.et_find);
// InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0));
yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0));
}
}, 200);
It works for me with 100ms delay, but failed without any delay or with only a delay of 1ms.
Commented part of code shows another approach, which works only on some devices. I tested on OS versions 2.2 (emulator), 2.2.1 (real device) and 1.6 (emulator).
This approach saved me a lot of pain.
To cause the keyboard to appear, use
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
This method is more reliable than invoking the InputMethodManager directly.
To close it, use
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
When nothing else works, force it to be shown:
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
And then later, if you wish to close it, in onPause() for example, you can call:
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
The following code is pillaged from the Google's 4.1 source code for SearchView. Seems to work, fine on lesser versions of Android as well.
private Runnable mShowImeRunnable = new Runnable() {
public void run() {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, 0);
}
}
};
private void setImeVisibility(final boolean visible) {
if (visible) {
post(mShowImeRunnable);
} else {
removeCallbacks(mShowImeRunnable);
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
}
Then in addition, the following code needs to be added as the Control/Activity is created. (In my case it's a composite control, rather than an activity).
this.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
setImeVisibility(hasFocus);
}
});
android:windowSoftInputMode="stateAlwaysVisible" -> in manifest File.
edittext.requestFocus(); -> in code.
This will open soft keyboard on which edit-text has request focus as activity appears.
I have had some recent luck in some simple cases with the code
below. I haven't finished all testing but....
EditText input = (EditText) findViewById(R.id.Input);
input.requestFocus();
input.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0));
input.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0));
And presto the keyboard shows up.
You can try to force the soft keyboard to appear, it works for me:
...
dialog.show();
input.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
And for Kotlin just use this extensions:
fun EditText.showKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
fun EditText.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.windowToken, 0)
}
For fragment, sure its working:
displayName = (EditText) view.findViewById(R.id.displayName);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Sometimes raukodraug's answer won't work. I've make it in this way with some trials and errors:
public static void showKeyboard(Activity activity) {
if (activity != null) {
activity.getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
public static void hideKeyboard(Activity activity) {
if (activity != null) {
activity.getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
And the EditText part:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(getActivity());
} else {
showKeyboard(getActivity());
}
}
});
showSoftInput was not working for me at all.
I figured I needed to set the input mode: (here in the Activity component in the manifest)
android:windowSoftInputMode="stateVisible"
To hide keyboard, use this one:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
and to show keyboard:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Kotlin extension for showing the keyboard on focus.
This is a combination of previous responses, which where either too long or incomplete.
This extension posts a runnable on the message queue which shows the soft keyboard after requesting focus:
fun View.showSoftKeyboard() {
post {
if (this.requestFocus()) {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
}
}
Call it from any view when needed afterwards:
editText.showSoftKeyboard()
Simply add this line in your EditText view:
android:isScrollContainer="true"
and TADA - keyboard began to show up automatically!
I had similar problem and discovered this simple and strange solution.
As already was mentioned here by user3392439, appearance of the keyboard upon focus somehow wierdly connected with presense of the scroll component in the XML file.
Even presence of another EditText view which comprises abovementioned line in same XML makes keyboard appear no matter which one of EditTexts is currently focused.
If you have at least one visible view comprising scroll component in your XML file - keyboard will appear automatically on focus.
If no scroll - then you need to click on EditText to make keyboard appear.
Believe or not my problem with Soft Keyboard was resolved when I discovered that the Activities animations can disable the Soft Keyboard. When you call the intent with the
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
and
overridePendingTransition(0, 0);
It can hide the Soft Keyboard and there isn't a way to show it.
I had the same problem in various different situations, and the solutions i have found work in some but dont work in others so here is a combine solution that works in most situations i have found:
public static void showVirtualKeyboard(Context context, final View view) {
if (context != null) {
final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
view.clearFocus();
if(view.isShown()) {
imm.showSoftInput(view, 0);
view.requestFocus();
} else {
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
#Override
public void onViewAttachedToWindow(View v) {
view.post(new Runnable() {
#Override
public void run() {
view.requestFocus();
imm.showSoftInput(view, 0);
}
});
view.removeOnAttachStateChangeListener(this);
}
#Override
public void onViewDetachedFromWindow(View v) {
view.removeOnAttachStateChangeListener(this);
}
});
}
}
}
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
I combined everything here and for me it works:
public static void showKeyboardWithFocus(View v, Activity a) {
try {
v.requestFocus();
InputMethodManager imm = (InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
a.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
It worked for me. You can try with this also to show the keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
code snippet . . .
public void hideKeyboard(Context activityContext){
InputMethodManager imm = (InputMethodManager)
activityContext.getSystemService(Context.INPUT_METHOD_SERVICE);
//android.R.id.content ( http://stackoverflow.com/a/12887919/2077479 )
View rootView = ((Activity) activityContext)
.findViewById(android.R.id.content).getRootView();
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
}
public void showKeyboard(Context activityContext, final EditText editText){
final InputMethodManager imm = (InputMethodManager)
activityContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (!editText.hasFocus()) {
editText.requestFocus();
}
editText.post(new Runnable() {
#Override
public void run() {
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
});
}
Inside your manifest:
android:windowSoftInputMode="stateAlwaysVisible" - initially launched keyboard.
android:windowSoftInputMode="stateAlwaysHidden" - initially hidden keyboard.
I like to use also "adjustPan" because when the keyboard launches then the screen auto adjusts.
<activity
android:name="YourActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"/>
Here's a more reliable solution i got from Square:
fun View.focusAndShowKeyboard() {
/**
* This is to be called when the window already has focus.
*/
fun View.showTheKeyboardNow() {
if (isFocused) {
post {
// We still post the call, just in case we are being notified of the windows focus
// but InputMethodManager didn't get properly setup yet.
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
}
}
requestFocus()
if (hasWindowFocus()) {
// No need to wait for the window to get focus.
showTheKeyboardNow()
} else {
// We need to wait until the window gets focus.
viewTreeObserver.addOnWindowFocusChangeListener(
object : ViewTreeObserver.OnWindowFocusChangeListener {
override fun onWindowFocusChanged(hasFocus: Boolean) {
// This notification will arrive just before the InputMethodManager gets set up.
if (hasFocus) {
this#focusAndShowKeyboard.showTheKeyboardNow()
// It’s very important to remove this listener once we are done.
viewTreeObserver.removeOnWindowFocusChangeListener(this)
}
}
})
}
}
Code credits from here.
just add android:windowSoftInputMode="stateHidden" in manifest file...
final InputMethodManager keyboard = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
None of the Answers worked for me. Here is a simple way.
searchEditText.setVisibility(View.VISIBLE);
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
searchEditText.requestFocus();
}
}, 400);
Just delayed the requestFocus() method for 400ms.
All solutions given above (InputMethodManager interaction in OnFocusChangeListener.onFocusChange listener attached to your EditText works fine if you have single edit in the activity.
In my case I have two edits.
private EditText tvX, tvY;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tvX.setOnFocusChangeListener(this);
tvY.setOnFocusChangeListener(this);
#Override
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(tvX.hasFocus() || tvY.hasFocus()) {
imm.showSoftInput(v, 0);
} else {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
};
I have observed that onFocusChange is triggered for tvX with hasFocus=true (keyboard shown) but then for tvY with hasFocus=true (keyboard hidden). In the end, no keyboard was visible.
General solution should have correct statement in if "show keyboard if EditText text has focus"
In your onResume() section of the Activity you can do call the method bringKeyboard();
onResume() {
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
bringKeyboard(yourEditText);
}
protected boolean bringKeyboard(EditText view) {
if (view == null) {
return false;
}
try {
// Depending if edittext has some pre-filled values you can decide whether to bring up soft keyboard or not
String value = view.getText().toString();
if (value == null) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
return true;
}
} catch (Exception e) {
Log.e(TAG, "decideFocus. Exception", e);
}
return false;
}
As I read on the official document, I think this is the best answer, just pass the View to parameter such as your EditText, but showSoftKeyboard seems like not working on landscape
private fun showSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
private fun closeSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
For Kotlin:
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
fun showKeyboard() {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
fun hideKeyboard() {
imm.hideSoftInputFromWindow(phoneNoInputTxt.windowToken, 0);
}
Then just call what you want!
I have clear button to clear EditText.
<Button
android:id="#+id/bClearText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dp"
android:onClick="clearEtSearch"
android:background="#drawable/delete" />
This method clears EditText:
public void clearEtSearch(View v) {
etSearch.setText("");
etSearch.requestFocus();
showKeyboard(etSearch);
}
I have taken code below from hiding and changed to show keyboard, but it is not working
private void showKeyboard(View view) {
InputMethodManager manager = (InputMethodManager) view.getContext()
.getSystemService(INPUT_METHOD_SERVICE);
if (manager != null)
manager.showSoftInputFromInputMethod(view.getWindowToken(), 0);
}
What I am doing wrong? Please give suggestions to correct my code.
I'm not sure but you can try to use Context.INPUT_METHOD_SERVICE instead of just INPUT_METHOD_SERVICE. Some are Forcing the Soft Keyboard open question for more.
You can also see How to show soft-keyboard when edittext is focused
See if following works:
public void clearEtSearch(View v) {
etSearch.setText("");
etSearch.requestFocus();
InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInputFromInputMethod(etSearch.getWindowToken(), 0);
}
Depending on your need you may try different constants of InputMethodManager like following:
public void clearEtSearch(View v) {
etSearch.setText("");
etSearch.requestFocus();
InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
or
public void clearEtSearch(View v) {
etSearch.setText("");
etSearch.requestFocus();
InputMethodManager manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(etSearch, InputMethodManager.SHOW_FORCED);
}
I haven't tried the code right not so not sure which one would work. See There are many related questions. Hope it works well for you.
Use this method and enjoy
public void showSoftKeyboard() {
try {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
showSoftInput() doesn't show the keyboard for me, but toggleSoftInput() does. I saw some other post that said to disable the hard keyboard when using the emulator, but I'm not using an emulator. I'm loading my APK on an actual device with no hard keyboard. Shouldn't both methods work? Why doesn't showSoftInput() work? I would like to explicitly associate the keyboard with a specific text field.
Doesn't work:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setText("textchange"); //i see the text field update
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
Works:
InputMethodManager imm = (InputMethodManager) getDelegate().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
It seems that the keyboard is initially displayed but hidden by something else, because the following works (but is actually a dirty work-around):
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
#Override
public void run()
{
editText.requestFocus();
imm.showSoftInput(editText, 0);
}
}, 100);
And when looking at logcat I suspect the cause behind this message hides the keyboard initially shown:
Hide Clipboard dialog at Starting input: finished by someone else... !
Show Keyboard + focus and also if you want to Hide the keyboard
#Override
public void onResume () {
super.onResume();
inputSearch.setFocusableInTouchMode(true);
inputSearch.requestFocus();
// Show Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputSearch, InputMethodManager.SHOW_IMPLICIT);
}
P.S inputSearch = (EditText) getSherlockActivity().findViewById(R.id.inputSearch);
// Hide Keyboard
InputMethodManager imm = (InputMethodManager) getSherlockActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
ShowSoftInput works if the imm's target view is same with the editText.
You can check this by imm.isActive(editText) or editText.isInputMethodTarget().
ToggleSoftInput is just toggling the keyboard of the current target of imm.
Target view of imm is set after the focus has been changed by editText.requestFocus(). I think some post processes exist between these two tasks since one post runnable was not enough. I tested double post and it worked for me.
editText.requestFocus();
editText.post(new Runnable() {
#Override
public void run() {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, 0);
}
}
});
}
});
The precise answer to this question why does showSoftInput doesnt work and toggleSoftInput does?
Is that the View to which you are trying to display the Keyboard doesn't have the focus. So to solve this problem and to use the method showSoftInput you will have to call the following to methods on your view:
setFocusable(true);
setFocusableInTouchMode(true);
Calling the above methods will make sure that when you click on the View retains and captures the focus.
showSoftInput() does not work when device has a hard (slide-out) keyboard
Android show softkeyboard with showSoftInput is not working?
Try this:
public void showTheKeyboard(Context context, EditText editText){
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
If this doesn't work read the tutorial from here
public void hideKeyboard() {
myTextView.setFocusable(true);
myTextView.setFocusableInTouchMode(true);
imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}
WORKS
public void hideKeyboard() {
imm.hideSoftInputFromWindow(myTextView.getWindowToken(), 0);
}
DOES NOT WORK
imm is dealt with earlier as I am using a Fragment so:
Declare imm in the Fragment
private InputMethodManager imm;
Then in the fragment add:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
imm = (InputMethodManager)
getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
}
He says after 3 to 4 hours of research and failures !!
Thanks user_CC ! :-)
Phil
I know this post is pretty old, but for those who came for answers from this date and none of the above worked. The code below worked for me on an Activity when an Alert Dialog popped up.
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
to show keyboard:
keyboard.toggleSoftInput(editText.getPaintFlags(), 0);
to hide keyboard:
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
I want to automatically show the soft-keyboard when an EditText is focused (if the device does not have a physical keyboard) and I have two problems:
When my Activity is displayed, my EditText is focused but the keyboard is not displayed, I need to click again on it to show the keyboard (it should be displayed when my Activity is displayed).
And when I click done on the keyboard, the keyboard is dissmissed but the EditText stays focused and y don't want (because my edit is done).
To resume, my problem is to have something more like on the iPhone: which keep the keyboard sync with my EditText state (focused / not focused) and of course does not present a soft-keyboard if there is a physical one.
To force the soft keyboard to appear, you can use
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
And for removing the focus on EditText, sadly you need to have a dummy View to grab focus.
To close it you can use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
This works for using it in a dialog
public void showKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public void closeKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
I had the same problem. Immediately after editText VISIBILITY change from GONE to VISIBLE, I had to set the focus and display the soft keyboard. I achieved this using the following code:
new Handler().postDelayed(new Runnable() {
public void run() {
// ((EditText) findViewById(R.id.et_find)).requestFocus();
//
EditText yourEditText= (EditText) findViewById(R.id.et_find);
// InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0));
yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0));
}
}, 200);
It works for me with 100ms delay, but failed without any delay or with only a delay of 1ms.
Commented part of code shows another approach, which works only on some devices. I tested on OS versions 2.2 (emulator), 2.2.1 (real device) and 1.6 (emulator).
This approach saved me a lot of pain.
To cause the keyboard to appear, use
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
This method is more reliable than invoking the InputMethodManager directly.
To close it, use
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
When nothing else works, force it to be shown:
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
And then later, if you wish to close it, in onPause() for example, you can call:
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
The following code is pillaged from the Google's 4.1 source code for SearchView. Seems to work, fine on lesser versions of Android as well.
private Runnable mShowImeRunnable = new Runnable() {
public void run() {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, 0);
}
}
};
private void setImeVisibility(final boolean visible) {
if (visible) {
post(mShowImeRunnable);
} else {
removeCallbacks(mShowImeRunnable);
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
}
Then in addition, the following code needs to be added as the Control/Activity is created. (In my case it's a composite control, rather than an activity).
this.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
setImeVisibility(hasFocus);
}
});
android:windowSoftInputMode="stateAlwaysVisible" -> in manifest File.
edittext.requestFocus(); -> in code.
This will open soft keyboard on which edit-text has request focus as activity appears.
I have had some recent luck in some simple cases with the code
below. I haven't finished all testing but....
EditText input = (EditText) findViewById(R.id.Input);
input.requestFocus();
input.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0));
input.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0));
And presto the keyboard shows up.
You can try to force the soft keyboard to appear, it works for me:
...
dialog.show();
input.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
And for Kotlin just use this extensions:
fun EditText.showKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
fun EditText.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.windowToken, 0)
}
For fragment, sure its working:
displayName = (EditText) view.findViewById(R.id.displayName);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Sometimes raukodraug's answer won't work. I've make it in this way with some trials and errors:
public static void showKeyboard(Activity activity) {
if (activity != null) {
activity.getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
public static void hideKeyboard(Activity activity) {
if (activity != null) {
activity.getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
And the EditText part:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(getActivity());
} else {
showKeyboard(getActivity());
}
}
});
showSoftInput was not working for me at all.
I figured I needed to set the input mode: (here in the Activity component in the manifest)
android:windowSoftInputMode="stateVisible"
To hide keyboard, use this one:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
and to show keyboard:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Kotlin extension for showing the keyboard on focus.
This is a combination of previous responses, which where either too long or incomplete.
This extension posts a runnable on the message queue which shows the soft keyboard after requesting focus:
fun View.showSoftKeyboard() {
post {
if (this.requestFocus()) {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
}
}
Call it from any view when needed afterwards:
editText.showSoftKeyboard()
Simply add this line in your EditText view:
android:isScrollContainer="true"
and TADA - keyboard began to show up automatically!
I had similar problem and discovered this simple and strange solution.
As already was mentioned here by user3392439, appearance of the keyboard upon focus somehow wierdly connected with presense of the scroll component in the XML file.
Even presence of another EditText view which comprises abovementioned line in same XML makes keyboard appear no matter which one of EditTexts is currently focused.
If you have at least one visible view comprising scroll component in your XML file - keyboard will appear automatically on focus.
If no scroll - then you need to click on EditText to make keyboard appear.
Believe or not my problem with Soft Keyboard was resolved when I discovered that the Activities animations can disable the Soft Keyboard. When you call the intent with the
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
and
overridePendingTransition(0, 0);
It can hide the Soft Keyboard and there isn't a way to show it.
I had the same problem in various different situations, and the solutions i have found work in some but dont work in others so here is a combine solution that works in most situations i have found:
public static void showVirtualKeyboard(Context context, final View view) {
if (context != null) {
final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
view.clearFocus();
if(view.isShown()) {
imm.showSoftInput(view, 0);
view.requestFocus();
} else {
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
#Override
public void onViewAttachedToWindow(View v) {
view.post(new Runnable() {
#Override
public void run() {
view.requestFocus();
imm.showSoftInput(view, 0);
}
});
view.removeOnAttachStateChangeListener(this);
}
#Override
public void onViewDetachedFromWindow(View v) {
view.removeOnAttachStateChangeListener(this);
}
});
}
}
}
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
I combined everything here and for me it works:
public static void showKeyboardWithFocus(View v, Activity a) {
try {
v.requestFocus();
InputMethodManager imm = (InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
a.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
It worked for me. You can try with this also to show the keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
code snippet . . .
public void hideKeyboard(Context activityContext){
InputMethodManager imm = (InputMethodManager)
activityContext.getSystemService(Context.INPUT_METHOD_SERVICE);
//android.R.id.content ( http://stackoverflow.com/a/12887919/2077479 )
View rootView = ((Activity) activityContext)
.findViewById(android.R.id.content).getRootView();
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
}
public void showKeyboard(Context activityContext, final EditText editText){
final InputMethodManager imm = (InputMethodManager)
activityContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (!editText.hasFocus()) {
editText.requestFocus();
}
editText.post(new Runnable() {
#Override
public void run() {
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
});
}
Inside your manifest:
android:windowSoftInputMode="stateAlwaysVisible" - initially launched keyboard.
android:windowSoftInputMode="stateAlwaysHidden" - initially hidden keyboard.
I like to use also "adjustPan" because when the keyboard launches then the screen auto adjusts.
<activity
android:name="YourActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"/>
Here's a more reliable solution i got from Square:
fun View.focusAndShowKeyboard() {
/**
* This is to be called when the window already has focus.
*/
fun View.showTheKeyboardNow() {
if (isFocused) {
post {
// We still post the call, just in case we are being notified of the windows focus
// but InputMethodManager didn't get properly setup yet.
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
}
}
requestFocus()
if (hasWindowFocus()) {
// No need to wait for the window to get focus.
showTheKeyboardNow()
} else {
// We need to wait until the window gets focus.
viewTreeObserver.addOnWindowFocusChangeListener(
object : ViewTreeObserver.OnWindowFocusChangeListener {
override fun onWindowFocusChanged(hasFocus: Boolean) {
// This notification will arrive just before the InputMethodManager gets set up.
if (hasFocus) {
this#focusAndShowKeyboard.showTheKeyboardNow()
// It’s very important to remove this listener once we are done.
viewTreeObserver.removeOnWindowFocusChangeListener(this)
}
}
})
}
}
Code credits from here.
just add android:windowSoftInputMode="stateHidden" in manifest file...
final InputMethodManager keyboard = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
None of the Answers worked for me. Here is a simple way.
searchEditText.setVisibility(View.VISIBLE);
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
searchEditText.requestFocus();
}
}, 400);
Just delayed the requestFocus() method for 400ms.
All solutions given above (InputMethodManager interaction in OnFocusChangeListener.onFocusChange listener attached to your EditText works fine if you have single edit in the activity.
In my case I have two edits.
private EditText tvX, tvY;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tvX.setOnFocusChangeListener(this);
tvY.setOnFocusChangeListener(this);
#Override
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(tvX.hasFocus() || tvY.hasFocus()) {
imm.showSoftInput(v, 0);
} else {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
};
I have observed that onFocusChange is triggered for tvX with hasFocus=true (keyboard shown) but then for tvY with hasFocus=true (keyboard hidden). In the end, no keyboard was visible.
General solution should have correct statement in if "show keyboard if EditText text has focus"
In your onResume() section of the Activity you can do call the method bringKeyboard();
onResume() {
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
bringKeyboard(yourEditText);
}
protected boolean bringKeyboard(EditText view) {
if (view == null) {
return false;
}
try {
// Depending if edittext has some pre-filled values you can decide whether to bring up soft keyboard or not
String value = view.getText().toString();
if (value == null) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
return true;
}
} catch (Exception e) {
Log.e(TAG, "decideFocus. Exception", e);
}
return false;
}
As I read on the official document, I think this is the best answer, just pass the View to parameter such as your EditText, but showSoftKeyboard seems like not working on landscape
private fun showSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
private fun closeSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
For Kotlin:
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
fun showKeyboard() {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
fun hideKeyboard() {
imm.hideSoftInputFromWindow(phoneNoInputTxt.windowToken, 0);
}
Then just call what you want!
I am trying to force the Soft Keyboard open in an Activity and grab everything that is entered as I want to handle the input myself, I don't have an EditText. Currently I have tried this but it does not work. I would like the Soft Keyboardto open below mAnswerTextView (Note: it is a TextView not EditText).
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(mAnswerTextView, InputMethodManager.SHOW_IMPLICIT);
how do I force the Soft Keyboard open
How do I gab everything that is entered so that I can handle each character. I would like to flush each character from the Soft Keyboard after I have handled it. ie, the user should not be able to enter whole words in the Soft Keyboard.
try this to force open soft keyboard:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
then you can to use this code to close the keyboard:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);
You'll probably need to have an editable text area of some kind to take focus. You can probably have one invisible or on a transparent background with no cursor, though. You may need to toy around with the focusability settings for the view.
Use a TextWatcher to check for edits to that EditText with addTextChangedListener, or if you need an even-lower-level hook, set the textview's key listener with its setOnKeyListener() method. See the KeyListener documentation.
Use this call to force the soft keyboard open:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);
and this one to close it:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
note that this is really not recommended - forcing the keyboard open is kind of messy. What's your use case that really necessitates your taking user input without a normal edit box and requires eating user input on a key-by-key basis without echoing it back?
To force the keyboard to open I used
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
it worked for me.
Sometimes the other answers won't work.
Here is another way..
It will force the keyboard to show when the activity starts by listening to the window focus. onWindowFocusChanged() it will clear and request focus of the EditText, then set the soft input mode to visible and set the selection to the text in the box. This should always work if you are calling it from the activity.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
mEditText.clearFocus();
mEditText.requestFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mEditText.setSelection(mEditText.getText().toString().length());
}
}
You may also need
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
}
}
});
Edit: I have also seen the keyboard not open inside nested fragments, beware of those kinds of situations.
if you want to control soft keyboard inside activity then use this code:
//create soft keyboard object
InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE);
//1.USE
your_view.setFocusableInTouchMode(true); //Enable touch soft keyboard to this view
//or
your_view.setFocusable(true); //Enable keyboard to this view
imm.showInputMethod(your_view, InputMethodManager.SHOW_IMPLICIT);
//2.USE show keyboard if is hidden or hide if it is shown
imm.toggleSoftInputFromWindow(your_view.getWindowToken(),InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//or
imm.toggleSoftInputFromWindow(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//3.USE (you cannot control imm)
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//4.USE (with Dialog)
Dialog d = new Dialog(this, android.R.style.Theme_Panel);
d.getWindow().setTitle(null);
d.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
d.setOnKeyListener(keyListener);
d.setCanceledOnTouchOutside(true);
d.setCancelable(true);
d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
d.show();
//to hide keyboard call:
d.dismiss();
//if you want get soft keyboard visibility call:
d.isShowing();
Sadly, as much as I'd have liked to up-vote one of the replies, none worked for me. It seems the solution is to wait for the layout phase to complete. In the code below, notice how I check if the showKeyboard method returns TRUE, and that's when I remove the global layout listener. Without doing this, it was hit and miss. Now it seems to work perfectly.
You need to do the below ideally in onResume()
#Override
public void onResume()
{
super.onResume();
ViewTreeObserver vto = txtTaskTitle.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
if (txtTaskTitle.requestFocus())
{
if (showKeyboard(getContext(), txtTaskTitle))
{
txtTaskTitle.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
}
});
}
public static boolean showKeyboard(Context context, EditText target)
{
if (context == null || target == null)
{
return false;
}
InputMethodManager imm = getInputMethodManager(context);
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
boolean didShowKeyboard = imm.showSoftInput(target, InputMethodManager.SHOW_FORCED);
if (!didShowKeyboard)
{
didShowKeyboard = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
}
return didShowKeyboard;
}
Working Great.........
edt_searchfilter_searchtext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
edt_searchfilter_searchtext.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getFragmentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchfilter_searchtext, InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
Below line call when you want to open keyboard
edt_searchfilter_searchtext.requestFocus();
if(search.getText().toString().trim().isEmpty()) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
}
I have tested and this is working:
...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Simply, using adding 2 lines will work like a charm:
If using XML
android:focusable="true"
android:focusableInTouchMode="true"
Else in Java:
view.setFocusableInTouchMode(true);
view.requestFocus();
You can use this KeyboardHelper.java class
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* Created by khanhamza on 06-Mar-17.
*/
public class KeyboardHelper {
public static void hideSoftKeyboard(Context context, View view) {
if (context == null || view == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static void hideSoftKeyboardForced(Context context, View view) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);
}
public static void hideSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public static void showSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
editText.requestFocus();
}
public static void showSoftKeyboardForcefully(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
editText.requestFocus();
}
}