I have multiple edit text and button on screen, on every button click there are some validation if validation is successful then i have to hide keyboard. I have tried so many code but nothing work.
Currently i am using,
InputMethodManager imm = (InputMethodManager) cntx.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
This is toggle, it opens if keyboard is hidden, but i wants to hide keyboard whether it is open or not.
Make one function like this, and Call from anywhere by passing context
like this :
public static void hideKeyBoard_WithView(Context context) {
// Check if no view has focus:
View view = ((Activity) context).getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Hope it will help you ! :) just try !
edittext.setFocusable(true);
edittext.setFocusableInTouchMode(true);
//try using this.. make the parameter as false when u dont required the keyboard & write the onclick method on edit text & make parameters true to get the focus again
Get current focussed view and using that view window token hide your keyboard.
View view = this.getCurrentFocus(); // get current focussed edittext
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Related
I'm trying to implement a feature where the virtual keyboard disappears from view when the user touches outside of it. It works, but when the edittext is not selected it causes the app to crash. I tried using a try/catch but that didn't help. From what I can tell, it's trying to close the keyboard when no keyboard is open. Any suggestions?
In the XML file:
android:onClick="hideKeyboard"
In the Java file:
public void hideKeyboard(View view) {
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
catch (Exception e) {
}
}
Replace you code
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
with:
InputMethodManager imm=(InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view3=getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view3==null){
view3=new View(this);
}
assert imm != null;
imm.hideSoftInputFromWindow(view3.getWindowToken(), 0);
I am using Search Widget to implement search interface in my app in action bar. As soon as the activity starts the keyboard pops up even if I have not interacted with the search widget. So for that I added the attribute in the activity tag in manifest file windowSoftInputMode giving value either stateHidden or stateAlwaysHidden but this did not worked. So I tried to do it programmatically using the hideSoftInputFromWindow method but this also did not worked.
What does this causes to pop up the keyboard even after the attributes are set not to show and how to solve this issue? Please help!
Try to Use this functions to show OR hide the keyboard:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Just set focus as false at first time when that screen loaded. By default when screen loaded, the focus goes to first edittext and Keyboard appears.set like this
view.setFocusable(false);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
and on clickListener of that view make that view as focused. by using
view.requestFocus();
You can try to set the focus on any other view of the activity like this:
view.requestFocus();
I spotted the problem and solved the issue. I was using android.widget.SearchView and changed it to android.support.v7.widget.SearchView, after which I didn't even need to use windowSoftInputMode in the manifest or the hideSoftInputFromWindow() method. Please note!
I am working on a project with an AutoCompleteTextView. I've put some buttons to work as a keyboard the problem is when i click(focus) on the autocompletetextview
the soft keyboard appears i dont want it to appear at all i have tried use this .
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
but this seems only to work if i put it in an onclick (button) and it is used to hide the keyboard while i need to disable it completely...any ideas?
public static void hideKeyboard(Activity activity) {
View view = activity.findViewById(android.R.id.content);
if (view != null) {
InputMethodManager imm =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
You can call this method on button click Maybe this helps you.
I've been trying to automatically show the keyboard with focus on a edit text when a fragment(that includes the edittext) is loaded.
I've tried using :
editTextPrice.requestFocus();
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(getView(), InputMethodManager.SHOW_IMPLICIT);
But this doesn't work the first time I load the fragment. The focus is set and I can see the cursor but no keyboard appears. If I close the fragment and add it again it works.
If tried everything from post() to a postpone it with a handler, to onResume,ect.
Does anybody have an idea what might cause this to happen?
Thanks in advance.
Best regards
let me explain why keyboard is not opening when we load a fragment the view cannot get focused, so we need set focus to the view in a separate thread, Then after we need to open keyBoard
void showKeyboard(Context mContext, View view)
{
if (mContext != null && view != null && view.requestFocus())
{
InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
This above function is to open keyboard
editTextPrice.postDelayed(() -> {
editTextPrice.requestFocus();
showKeyboard(getContext(), editTextPrice);
}, 300);
android:focusable="true"
android:focusableInTouchMode="true"
And
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
you try using 2 function below:
fun hideKeyBoard(context: Context, view: View) {
val inputMethodManager = context.getSystemService(
Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
fun showKeyBoard(context: Context, view: View) {
val inputMethodManager = context.getSystemService(
Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
Do not request focus from java code. Do it from the xml itself like
android:focusable="true"
also do the following in the manifest file
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysVisible" />
I am using a FrameLayout to show an EditText and a ListView (with checkboxes) alternately. When showing an EditText, I would like the soft keyboard to be shown. And when showing the ListView, I would like the soft keyboard to be hidden. Now usually a focus is needed in order to hide the soft keyboard. When my ListView gets shown, then getCurrentFocus() returns null. Is there a way to hide the soft keyboard, without having a focus?
I am showing the soft keyboard like that:
public static void requestFocusAndMoveCursorToTheEndAndShowKeyboard(final EditText editTextParam, final Activity activityParam) {
if (editTextParam == null) {
return;
}
if (editTextParam.requestFocus()) {
editTextParam.setSelection(editTextParam.getText().length()); // move Cursor to the end of the EditText
InputMethodManager imm = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
And I am trying to hide the soft keyboard like that:
public static void hideSoftInputKeyboardFromWindow(Activity activityParam) {
if (activityParam == null) {
return;
}
View view = activityParam.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Make use of .clearFocus(); on edittext when you don't want a focus on it.
In your AndroidMenifest.xml add this:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
Try this:
Write following method to youractivity or to your utility class
/**
* Hide soft keypad
*
*/
public static void hideKeyboard(Activity activity, View v) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
In your hideSoftInputKeyboardFromWindow method, try:
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
instead of
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Edit: ok same answer as Dorami
How do you show them alternately? Using different fragments? Or do you simply inflate different layouts? Provide more details with your complete code
Thank you for your answers. Finally I got it solved using a View.OnFocusChangeListener for the EditText, like described here:
Hide soft keyboard on losing focus