I have a request from a client which says something like this: after a user inserts his name, on tap out validate with server.
I never heard about tapping out from a field in Android yet. (tap out means when you are writing something into an EditText, then you click on other view, hide keyboard and call focus change listener).
Do you have any idea about this implemented in Android? Thanks.
Update, after marked duplicated: I already did my research for this a lot, this is why I asked here for an opinion.
The above case is just an example. My main interest is regarding tapping out from EditText (TAP on another view with NO action - e.g. a TextView with no clickable function). I hope it's clear now.
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your focused view.
InputMethodManager methodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (methodManager != null) {
methodManager.hideSoftInputFromWindow(mEditText.getWindowToken(),0);
}
Related
I want to be able to know when a user is done entering text in an EditText control. I'm thinking maybe it's best to know when the keyboard is closed or something similar. This is using Kotlin on Android app. I'm not sure why it's so hard to find basic answers like this. Maybe I'm searching with the wrong question (new to Android dev).
Using keyboard close as an indicator that the user finished entering text is a bad idea (the user might open the keyboard again to enter more text). A better solution would be to explicitly require for the user to indicate that he has finished entering the data.
You could use a "submit" button.
You can also set the android:imeOptions of EditText to actionDone and set a listener on the EditText.
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do your stuff
return true;
}
return false;
}
});
Update - Assuming keyboard close as the indicator is bad for a couple of reasons,
There is no 'proper' way to monitor the soft keyboard. You could try listening to the focus of the EditText or you could use the height difference to guesstimate whether the soft keyboard is open or closed(the option used in most keyboard listener libraries). But these aren't reliable and might break in production.
It's an 'unexpected' application behavior for the user. For example, the keyboard can be removed by pressing the back button. In general, the user would expect that the action would not proceed if the back button is pressed. But if you listen to keyboard close, then it would end up resulting in poorer UX.
There are no actual reasons why you would want to use keyboard close as the trigger. If you want to perform the action as the user types, then you should use TextWatcher, otherwise stick to explicit confirm options.
use onFocusChangeListener to know if the user has finished to add text and has leave the textInput focus.
Example
editText?.onFocusChangeListener =
View.OnFocusChangeListener { _,
hasFocus ->
if (!hasFocus) {
// code to execute when EditText loses focus
}
}
Im trying to resolve a usability problem in my app, im using Xamarin Android but for this question i think we can ignore Xamarin.
I have a list where each ítem has a edittext like a counter for each ítem.
The user is capable of changing that value by clicking on + and - buttons or directly by writing in the edittext with the softKeyboard.
My problem shows when the user opens the keyboard, change the number, but do not click on done but instead just close the keyboard using the backbutton.
Im using something like this to get the counter change.
editCount.EditorAction += delegate (object sender, TextView.EditorActionEventArgs e) {
if (e.ActionId == Android.Views.InputMethods.ImeAction.Done)
{
Of course, thats not being called if the user simply close the keyboard instead of hitting done.
I know i could use TextChanged instead of EditorAction but it will be a real nightmare to make those changes to this app...
Just forcing the user to hit the done button to close the keyboard should be enought, much better if i can restore the previous value of the edittext in case they just close the keyboard.
How can i achieve something like this?
I know that to hide soft keyboard I need to use code like this:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
My problem is that I starting the ACTION_SEND intent, and use Twitter app to handle it. I pass a message to tweet it. But if the user does not confirm the message, but clicks ActionBar back button, the Twitter activity is finished, and my app lication comes back to the front. But the soft Keyboard, called by Twitter does not hide. I have no idea how to get Twitter's WindowToken. Could anybody help me?
Another way is to do the same in AndroidManifest.xml file. You can annotate your activity with the following line:
android:windowSoftInputMode="stateAlwaysHidden"
which means your activity will always hide keyboard when receives focus.
I have found an unswer.
I had to add this code:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
in the onResume() method
I want to show Google Voice Typing IME on my EditText by clicking on Button. So, according to this article and source code I should write this code
inputMethodManager.setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype)
The problem is: where to find appropriate token. In the source code I saw this
mInputMethodService.getWindow().getWindow().getAttributes().token
It works great, but this code located in InputMetodService superclass, so it has access to InputMethodService instance. But i don't (unfortunately :) ).
Please tell me if you have any suggestions. Thanks.
NOT WORKS: EditText.getWindowToken()
Due to security reasons android doesn't allows application to change the inputMethod type. The article you mentioned is for integrating the google IME in custom implemented IME, it is not applicable for applications.
you can check documentation of InputMethodManager here
A client application can ask that the system let the user pick a new IME, but can not programmatically switch to one itself. This avoids malicious applications from switching the user to their own IME, which remains running when the user navigates away to another application. An IME, on the other hand, is allowed to programmatically switch the system to another IME, since it already has full control of user input.
you can prompt the user to switch to new IME in your onClick Callback like this:
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
I hava a GLSurface, a KeyListener and TouchListener, I added the listeners to the suface and when the user click on the screen open the soft keyboard using this code:
InputMethodManager inputMethod = (InputMethodManager)((Activity)game).getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethod.showSoftInput(game.getGraphics().getView(), 0);
"game.getGraphics().getView()" returns the GLSurface. This code is working.
But when I press any key on soft keyboard, the listener does not receive any event. Why?
How can I fix it?
I know the OnKeyListener does not necessary receive event from software, but what I need to do?