Prevent editText.setText while EditText is being modified by soft input keyboard - android

I have an EditText in my application. This editText is being updated through code for every 10 seconds using editText.setText method. I want to stop this updation when user opens soft keyboard. When user completes his typing action (Press done) button I want to resume updation from editText.setText.
I have tried InputMethodManager isActive(View) to check whether my EditText is currently active while using editText.setText method. This perfectly works when keyboard is displayed. But when user clicks done button and soft-keyboard gets hidden, isActive(View) still gives true and my editText.setText is not being called.
Below is my code which updates editText every 10 seconds.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(!imm.isActive(editText)){
editText.setText("10.2");
}
So this imm.isActive(editText) returns true even after user press done button of soft keyboard.
Please suggest any way to get this done.

Solution 1
Create a boolean control variable isKeyboardVisible and set it to true when you show the keyboard, false when you hide it. Check its value before calling setText().
Solution 2
An alternate, somewhat hacky solution would be to enclose both of the portions that change the EditText value within a synchronized block. This way each portion would wait for the other to complete before executing.
Solution 3
You can listen to the EditText's focus change events and react to them. They tend to coincide with the soft keyboard showing/hiding itself.

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

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

How to detect when user leaves an EditText?

I want an EditText to lose focus the moment the user touches other UI elements in the Activity like check boxes or really touches anywhere outside the EditText within the Activity, but this is not happening. The focus only gets changed when the user starts typing into another EditText. Not for example when they click on an Checkbox or when they click on other areas of the screen.
It sounds like you need to add the following two lines to all of the widgets in your layout:
android:focusable="true"
android:focusableInTouchMode="true"
That way they can all receive focus (normally only certain ones can) and you can attach onFocusChangeListener to the EditText in question and you should achieve the desired results.
Of course, this could also play havoc with your other widgets (not knowing what they are I can't be certain), but it's worth a shot.
Could you not just force close the android soft keyboard on click event of another View?
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
The toggle call literally does just that - if its open, it will close, if its closed, it will open!

Dismiss Soft Keyboard after Hardware Search is performed

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"

Android keyboard

My application starts with a bunch of text input fields, and I want that when starting up the the application. The virtual keyboard isn't open, but opens only when I click on one of the textinput fields.
How do I do this?
In your onCreate method you could get your first text view and call requestFocus() on it. This ought to focus this field when the activity starts and bring up a virtual keyboard if needed.
If you want the keyboard not to appear on startup, request focus for a non-text element like a button.
You should leave the input method to the user. They might be using a physical keyboard or maybe even something like speech to text.
I've used this approach to hide the keyboard after the user searches. You could use this in our onCreate method:
Close/hide the Android Soft Keyboard
Quote from Reto Meier's accepted solution:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

Categories

Resources