I have an app that contains at some point a FragmentActivity. This FragmentActivity contains some Fragments (FragmentA, FragmentB, etc). On each of these fragments, there are one or more EditText.
I'm trying to handle the keyboard properly. I want to make the Keyboard appear or disappear whenever I want.
For Example, When the FragmentA is created, I want to open the keyboard on an EditText. Then, three options possible for the user :
He clicks on a button that goes to the next fragment
He clicks on the Action Send of the Keyboard(that does nothing (on purpose) but close the keyboard normally) and then he can click on the button from the view
He clicks on the back button of the phone. In this case, it closes the keyboard and then he can click on the button form the view.
My problem is, whenever the user closes the keyboard by clicking on the back button of the phone, Android thinks that he doesn't want to see the keyboard ever in the activity. So when the FragmentB is created, I can't programmatically show the keyboard (Using InputManager btw) on a EditText from this Fragment.
Then, a second problem is when i click on the edittext to get the focus, in Android 4.x, the keyboard shows again, no problems, but with Android 2.x, it's impossible to have the keyboard shown again even if the focus is on the edittext ! It's killing me.
It appears that once the user has explicitly close the keyboard with the back button in one activity (even a fragment activity), you cannot show it again.
Does anyone have a solution ? Maybe playing with the flags in InputManager ? I didn't get all of them and what they do.
Try this:
Show keyboard:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
EditText view = getCurrentFocus();
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
Hide keyboard:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Hope it helps ;)
Related
I have an activity that launches a fragment whose main purpose is to present the user with an Imagebutton that they can click to launch the soft keyboard and type in whatever text they like. I need to grab each key as it is pressed on the soft keyboard. However, the user is not typing into an EditText view so I am struggling a bit with this. I can successfully display the soft keyboard when the user clicks the ImageButton using the code below...
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
However, I have not been able to find an example or a method that would let me grab the keystrokes from the soft keyboard without an EditText. I assume this must be possible I just don't know how to go about it.
Maybe it helps if you implement your own InputMethodService? You can overwrite the onUpdateSelection() method, which is called every time when a key is touched on the softkeyboard.
In my android application, I have a dialog in one fragment and this dialog contains a edittext, I use showSoftInput to let soft keyboard showing. But when I tap outside the dialog(not tap the keyboard button or edittext), the dialog should dismiss and it does. The weird thing is when the phone in portrait view the softkeybaord will not dismiss following the dialog but in landscape view the softkeyboard dismiss.
Anyone meets this case? And I find this in the android developer website:Note: Once the input method is visible, you should not programmatically hide it. The system hides the input method when the user finishes the task in the text field or the user can hide it with a system control (such as with the Back button)
I got one more solution to hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
Add this on tap outside
I have found my root reason, it is because the host activity set:
android:windowSoftInputMode="stateHidden|adjustResize".
After I remove this, my problem is solved.
I am using the soft keyboard in Android Studio in order to display user input on the screen. The program has a button, that when clicked, displays whatever it is that the person typed in. As soon as I click on the EditText field, the soft keyboard pops up and I type in the input. The only problem is that I dont know how to close it after I'm done. My Textfield, which displays the input, is at the bottom of the screen so I can't see it if the keyboard remains open. Shouldn't there be some type of button? Is there a way of solving this??? Thank you.
in the OnClickListener of the button, which you are using to display the text entered when clicked, you can add some thing which closes the keyboard when clicked.
Add this inside the onClickListener
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
so when you click the button, it hides the keyboard and displays the text entered.
Even if my SearchView is not in focus (ie. the user has already pressed the "Search" button earlier to submit their query), when I press the cancel (the X) button on my Android SearchView, the soft keyboard comes back up into view.
My thinking is that if the user doesn't already have the keyboard on the screen, then they just want to clear the filter/search box. If they want to clear the filter and type something different they can tap it again.
However, if they are typing into the box and make a mistake I would expect the keyboard to remain in view (because the search view already has focus).
So in a nutshell I want:
If the user is typing in the search view and taps cancel/clear, then the keyboard stays in focus.
If the user is not currently typing in the search view (ie. the keyboard has disappeared from view), then tapping the clear button should just clear the query and NOT bring the keyboard back into view.
I know I can use the setOnCloseListener() event to hook into when the clear/close button is pressed, but I don't know how to stop it from showing the soft keyboard as mentioned in point #2.
EDIT:
Maybe there is a way I can have the search view "lose focus"?
How do I achieve this result? Thanks.
You can lose focus by doing the following:
searchView.clearFocus();
You can also force hiding the keyboard on any event you want with the inputManager.
For example:
InputMethodManager inputManager = (InputMethodManager) this
.getSystemService(Context.INPUT_METHOD_SERVICE);
//check if no view has focus:
View v=this.getCurrentFocus();
if(v==null)
return;
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
I have this issue: in my app, when user taps on EditText bar, keyboard pops up. After that, it is impossible to get rid of keyboard. When back button is pressed, whole application just turn off.
How can I make sure, that when user taps on some other object (not EditText), keyboard will be removed? Or at least, how to make it possible to hide keyboard by tapping back button?
Thanks.
in xml for EditText this will make keyboard dismiss when press enter on keyboard
android:imeOptions="actionDone"
You can hide the keyboard simple by overriding onBackPressed in your Activity and using the following code:
InputMethodManager inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(anyView.getWindowToken(), 0);
Note that anyView can be any view that is attached to your current window.
You can see it working in my app called Magic Annotator. See method hideSoftKeyboard()