I'm opening a Dialog
from within an Activity
. When the dialog opens, I call
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
The problem is, when I close the dialog either by hitting a cancel button or clicking outside the dialog, the keyboard switches to a text keyboard and doesn't go away util I click the hardware back button. How can I dismiss the keyboard when the dialog is dismissed, and focus is returned to the previous window?
Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.
Setting android:isScrollContainer = "false" inside the ScrollView worked for me. According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.
Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden. The back button may be a physical button or on the touch screen. To bring the keyboard back into view, tap the typing area.
setShowSoftInputOnFocus(false); to disable the software keyboard showing when the EditText is touched. the hideKeyboard(this); call in OnCreate to forcible hide the software keyboard.
I guess that this method of the activity can be useful to you.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if(hasFocus)
{
Toast.makeText(MainActivity.this, "has focus", Toast.LENGTH_LONG).show();
// write code to remove keyboard
}
}
in AndroidManifest.xml, set this property in your Activity that show the Dialog
android:windowSoftInputMode="stateAlwaysHidden"
Note! not stateHiddent, is stateAlwaysHidden. It will automatically hide soft keyboard on Dismiss of Dialog.
Hope that save your life.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With