Dismiss Soft Keyboard after Hardware Search is performed - android

I have an Activity which performs searching within my app. I have this Activity set as the handler for the hardware search button. This all works great.
If a user navigates to my Activity, enters a search query and then clicks on the "Search" button then I dismiss the soft keyboard via:
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(etQuery.getWindowToken(), 0);
etQuery is my EditText field.
However, if a user taps the hardware search button, then the search input overlap is shown, and the user is allowed to enter text, which they do, upon clicking "Go" my Activity gets the input and performs the search successfully. However, the soft keyboard is still visible.
Even though I do call the above keyboard dismissal code, it doesnt actually work. I assume because the window [token] that initiated the soft keyboard is NOT in fact the etQuery EditText - it was initiated by the Hardware Search facility. Thus asking it to close based on the EditTexts token has no effect.
At this point, I don't really care who opened the soft keyboard, I just want it to close.
How can I force the soft keyboard to close regardless of who opened it?

I solved this by adding the following attribute to the activity in the manifest:
android:windowSoftInputMode="stateHidden"

Related

Cancel on SearchView causing the soft keyboard to appear

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);

Dismissing a DialogFragment when the soft keyboard is hidden

I have a simple DialogFragment that contains an EditText. When the DialogFragment is created the soft keyboard is shown immediately and the EditText gains immediate focus by using:
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(
LayoutParams.SOFT_INPUT_STATE_VISIBLE);
In fact, what I have is essentially like the example given in this blog:
http://android-developers.blogspot.co.uk/2012/05/using-dialogfragments.html
When the back button is pressed, I wish for the DialogFragment to be dismissed. What actually happens is that the first back button press causes the soft keyboard to be hidden. A further back press is required to dismiss the DialogFragment.
I was quite surprised that there doesn't seem to be a simple API solution for this (such as setting a flag) as I'd have thought it'd be a common requirement.
Having searched on SO the best option seems to be to detect when the soft keyboard has been hidden, and then call dismiss() on that event. Such possible solutions for detecting the soft keyboard is hidden are:
EditText with soft keyboard and "Back" button
How to check visibility of software keyboard in Android?
Before I go ahead and use one of the above solutions, is there any other means I should consider dismiss of the entire DialogFragment and soft keyboard with one hit of the back button?
Why not using a cancel button instead of exploiting the back one?

Set android focus on touch?

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()

android make soft keyboard always visible

My activity has an EditText field and I want Soft Keyboard to be always visible in the activity. I set
android:windowSoftInputMode="stateVisible"
in the manifest, but it results in animated appearance of Soft Keyboard when the activity starts. I need SoftKeybard to appear in full size at exactly the same time when activity starts, without animation.
I don't think this is possible.
You can make the keyboard to automatically show when the activity or fragment is created but the keyboard will be dismissed when the user presses the backbutton.

Why does the IME on my android application de-associate from the EditText it should be editing?

I've got an Android application I'm writing. It has a ListActivity in it that's all set up to load my data using this layout for each item.
My data Adapter binds with no problem and I've set it up so that when an item is selected from the list this method is called.
private void showPasswordBox(View v) {
EditText passwordBox = (EditText)v.findViewById(R.id.hidden_box);
Button passwordSubmit = (Button)v.findViewById(R.id.hidden_box_submit);
passwordSubmit.setText("Login");
passwordSubmit.setVisibility(View.VISIBLE);
passwordBox.setFocusableInTouchMode(true);
passwordBox.setHint(R.string.password);
passwordBox.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
passwordBox.setVisibility(View.VISIBLE);
}
This has the effect of displaying and EditText and a Button (hidden_box and hidden_box_submit in my layout). Which is just what I wanted.
However a problem occurs when the user taps on the newly visible EditText (hidden_box). The IME pops up and hidden_box immediately loses focus. Consequently anything typed on the IME does not appear in the EditText. Instead it's doing this weird thing where anything typed appears above the keyboard in grey letters and remains at the bottom of the screen when the IME is dismissed. It's like the IME is typing into it's own temporary invisible box.
If the user taps on the EditText after the IME is already showing then the application behaves as it should. Anything typed on the IME appears in the EditText and remains when the IME is dismissed.
It seems to me that when the IME pops up (which it does immediately when the user taps on the EditText, as it should) it completely de-associates with my application and does its thing in IME la-la land unless I direct it to the EditText by tapping on the EditText before typing and dismissing the keyboard. How do I make it behave normally so it types directly into the EditText as soon as it pops up?
passwordBox.setFocusableInTouchMode(true);
Hope this will work. Does it?

Categories

Resources