How to detect when user leaves an EditText? - android

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!

Related

Getting keystrokes from the soft keyboard from within a fragment

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.

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

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

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.

Keyboard doesn't show up when EditText is in focus

I want to do the following.
User should be able to input one letter (only letter) from standard keyboard (hardware or software). If he is typing another letter, then the previous letter should be replaced with this one. So only the current letter should be displayed. User should be able to dismiss this dialog and get back to activity. And if he clicked "done" button in keyboard the activity should know what letter he entered.
So I thought about alert dialog and edit text with some extensions to display only current char. This is easy.
However, and this gets me mad already, although edit text is in focus the keyboard does not appear on the screen until edit text is clicked. Why is that so?
It should be, should it not?
I won't take something like the following for the answer, because I shouldn't have to do it manually. It should be automatic. Besides, I'm not sure how this would work with a hardware keyboard.
InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(YourEditText.getWindowToken(), 0);
I want to know why exactly the keyboard is not shown after edit text has focus?
And what should I do to get this functionality without manually enabling and disabling software keyboard.
Use this.
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
For your information, see this related question: Why the soft keyboard shows or not when an activity starts?. You can read it as an alternative by enclosing your EditText into a ScrollView.
But it's not more satisfying than the well known workaround (manually enabling the software keyboard) because we don't understand why it works better in this case...

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

Categories

Resources