Prevent android webview soft keyboard to lost focus - android

I have a problem since a long time and I have not find a way of solving it.
My app as a webview that loads an url where the user can fill certain text inputs, also, the app produces sounds in certain situations(to help/assist the users), the problem is that, sometimes when the user is writing in an input and some sound is triggered the soft keyboard hides because the input lost its focus.
This is quite annoying to the users because they have to touch the input again to continue writing.
Any ideas about how to solve it?

I was confused on this same issue but I learned that it is common practice to call search.blur() (assuming we are talking about a search text input field) when a window.resize event is triggered for desktop, but sometimes that same code causes issues on mobile because the virtual keyboard triggers a resize event as well. If this is the case, you may have to use something like modernizr to determine if you are in a mobile device context and if so don't call that search.blur(). Search your code base for the blur() pattern to see if that is what is happening for you.
To fix, try something like this:
window.onresize = function () {
if (!isMobile) {
search.blur()
}
}

Related

Xamarin Android change accessibility focus

In Android Studio, using Java, to set the accessibility focus on a certain view after, for example, pressing a button, you have to do myview.sendAccessibilityEvent(AccessibilityEvent.WINDOWS_CHANGE_ACCESSIBILITY_FOCUSED). In Xamaring, using C#, the closest thing that I found is myview.SendAccessibilityEvent(EventTypes.ViewAccessibilityFocused); but it doesn't work that good, infact, it only read out the myview accessibility name, without actually setting the focus on that, the focus remain on the button pressed. I also tried myview.SendAccessibilityEvent(EventTypes.ViewFocused); but it doesn't work either.
Another thing that I found is myview.SendAccessibilityEventUnchecked(AccessibilityEvent.WindowsChangeAccessibilityFocused); but I got an error saying Argument 1: cannot convert from 'Android.Views.Accessibility.WindowsChange' to 'Android.Views.Accessibility.AccessibilityEvent' even if it is said that SendAccessibilityEventUnchecked takes an AccessibilityEvent as argument.
Any idea on how to solve?
Or any idea on how to set the accessibility focus to a certain view after some event?
(Note if needed, I am just testing it on Android native, than I will need to use that in a Forms project using Dependency Service)
WINDOWS_CHANGE_ACCESSIBILITY_FOCUSED is a sub type for TYPE_WINDOWS_CHANGED, which should only be dispatched by the system. And the accessibility focus won't be changed by sending accessibility event.
Actually, send accessibility event triggers Accessibility Service, and you can change the accessibility focus in the service's onAccessibilityEvent method with:
public override void OnAccessibilityEvent(AccessibilityEvent e)
{
//you can use event type and source node to see if the event is sent by you
AccessibilityNodeInfo sourceNode = e.Source;
AccessibilityNodeInfo targetNode = sourceNode.FocusSearch(FocusSearchDirection.Down);
targetNode.PerformAction(Android.Views.Accessibility.Action.AccessibilityFocus);
}
See more information about Accessibility Focus and Input Focus here.
Problem solved, the right way to achieve that should be myview.SendAccessibilityEvent(EventTypes.ViewHoverEnter) that has the same integer value of the Android value WINDOWS_CHANGE_ACCESSIBILITY_FOCUSED.
Another way is to do myview.SendAccessibilityEvent((EventTypes) (int) WindowsChange.AccessibilityFocused) so you have to do a double cast, int and EventTypes, on WindowsChange.AccessibilityFocused because WindowsChange is a different enum from EventTypes.
I leave the github issue that I opened where I got these answers git issue.

Android Accessibility Service Focus EditText

I am writing an accessibility service. I've been trying to focus EditText fields in a way that pops up the software keyboard and I have been unsuccessful. I am able to get the cursor into the EditText field, but the soft keyboard doesn't pop up. Given EditTextNode is an AccessibilityNodeInfo that I have gotten from various accessibility events and that said nodeInfo isEditable, isFocusable, isVisibleToUser and isClickable when I attempt all of these actions and they all return true upon completion.
editTextNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
In my mind the above should simply work, and the fact that it does not is a bug in the Accessibility API, if not for my entire android version, at the very least on my device (Droid Ultra, Android 4.4.4). I have also tried:
editTextNode.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
Puts focus into the field so I can see the input cursor, but no keyboard.
editTextNode.performAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Doesn't really do anything unless talkback is enabled.
editTextNode.performAction(AccessibilityNodeInfo.ACTION_SET_SELECTION, someBundle);
Along with appropriate arguments this will work, but only if there is already text in the editText field. I need to pull the keyboard up on empty text fields as well. Yes, I tried inputing 0,0 for start and end text selection arguments. The SET_SELECTION method only works with text in the field!
This has been frustrating me for a couple days, hopefully you guys can help. Though I believe I've stumbled onto a bug and am going to have to wait for Android to update. Because the ACTION_CLICK method should really be all that is needed. But, I could be missing something silly, Accessibility API Doc is somewhat scant. Am willing to attempt anything.
To restate #alanv's comment in answer form:
You are correct, it is a bug in accessibility services prior to API 21.

Is it possible to define a suggestion list in webview applications?

When entering a string in a text type input element in a webview based app, a list of possible words is showed above the virtual keyboard.
In my application the user is not allowed to input arbitrary words, instead only words and phrases from a database are legal.
So, I want to set the list of words from my phonegap app and disable the automatic creation of this list.
Q: Is this possible to create the list - and how?
(This is a phonegap app and I am testing on android, but this might be a problem for webview / text input fields on different platforms)
EDIT:
Just found out (yes, I'm new to android development), that one can disable the suggestion list with: Settings / Language and Keyboard / Android keyboard / Show suggestions.
But of course, this setting should be made only for the app, from inside the app, without user interaction and not changing anything outside the app.
Any chance to get this done?
EDIT 2:
Instead of disabling the suggestion list I tried to use it.
The displayCompletions method of InputMethodManager sounded promising, so I tried the following code:
...
// data member
InputMethodManager mInputMethodManager = null;
...
...
// initialized
mInputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
...
public void showSuggestions(String[] words) {
Log.d(TAG, "showSuggestions (in Java!): " + words.length); // yes, this code is executed
CompletionInfo[] completions = new CompletionInfo[words.length];
for (int i=0; i<words.length; i++) {
completions[i] = new CompletionInfo(i, i, words[i]); // no idea, what the 2 extra integers mean
}
mInputMethodManager.displayCompletions(mView, completions);
}
The method is executed, whenever a new list is available (after the input has changed and the server has answered a request).
However, nothing has changed - the builtin mechanism is still doing its disadvantageous work.
Why don't you use something like the auto complete in jQuery UI.
http://jqueryui.com/demos/autocomplete/
Also, it's not too hard to whip this up yourself in JavaScript.
http://www.javascript-examples.com/autocomplete-demo/
In case there are different variants of virtual keyboard(swype and others) there might be no list of possible words. So, I suppose there is no general solution for not showing this list.
There is a parameter http://developer.android.com/reference/android/widget/TextView.html#attr_android:editorExtras editorExtras which defines parameters passed to input method implementation, but I'm not sure how would you use it in PhoneGap application
Edit:
I suppose you can extend default input method and make it not to show suggestions
inputmethod reference http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html
Providing Custom Software Keyboards
If you are interested in developing your own software keyboards, we highly recommend the following references:
IMEs are implemented as an Android service. Begin by reviewing the Android packages called android.inputmethodservice and android.view.inputmethod, which can be used to implement custom input methods.
The SoftKeyboard sample application in the Android SDK provides an implementation of a software keyboard.
The Android Developer technical articles on onscreen input methods and creating an input method.
found it here http://e-university.wisdomjobs.com/android/chapter-946-288/handling-advanced-user-input.html

How to perform Redo Undo operation in EditText

I want to know is there any method or any link or tutorial to perform redo undo operation in Android edittext. If any one knows than please let me know.
Quick note on the Antti-Brax/Divers(Kidinov) solution. It works great, except if you try to use it with a TextView post-API 23, you'll run into problems, because guess-what, Google actually added a hidden UndoManager (android.content.UndoManager) and didn't document it or make it obvious it was there. But if you have a hard/bluetooth keyboard in Marshmallow or Nougat and hit ^Z or SHIFT-^Z, you'll get undo/redo.
The problem comes if you're already using Antti-Brax's class with an EditText, and you also hook it to ^Z and shift-^Z, you'll run into problems with anyone using a hard keyboard. Namely the ^Z will trigger BOTH the native and Antti-Brax's undo, leading to two undos simultaneously, which isn't good. And after a few of them, you'll probably get a Spannable out of bounds crash.
A possible solution I found is to subclass the TextView/TextEdit/whatever and intercept the undo/redo calls from the TextView so they don't run as follows:
#Override
public boolean onTextContextMenuItem(int id) {
int ID_UNDO, ID_REDO;
try {
ID_UNDO = android.R.id.undo;
ID_REDO = android.R.id.redo;
} catch (Resources.NotFoundException e) {
ID_UNDO = 16908338; // 0x1020032
ID_REDO = 16908339; // 0x1020033
}
return !((id == ID_UNDO) || (id == ID_REDO)) && super.onTextContextMenuItem(id);
}
Those magic id numbers were found here, and are used only as a backup if the android.R.id.undo values aren't found. (it also might be reasonable to assume that if the values aren't there the feature isn't there, but anyway...)
This is not the best solution because both undo trackers are still there and both are running in the background. But at least you won't trigger both of them simultaneously with ^Z. It's the best I could think to do until this gets officially documented and the getUndoManager() methods of TextView is no longer hidden...
Why they made a feature you can't turn off (or even know if it was there or not) "live" in released Android I can't say.
I just opened an issue on Android's issue tracker if anyone wants to follow this.
There is an implementation of undo/redo for Android EditText in
http://credentiality-android-scripting.googlecode.com/hg/android/ScriptingLayerForAndroid/src/com/googlecode/android_scripting/activity/ScriptEditor.java
The code works but does not handle configuration changes properly. I am working on a fix and will post here when it is complete.
My Google search was :-
android edittext onTextChanged undo
I know this is an old question, but as there is no accepted answer, and this is an issue I've tackled myself from many angles, I'd like to add my solution in case it helps anyone. My answer is probably most relevant to large (1,000words+) volumes of text editing apps that require this feature.
The simplest way to resolve this problem is to make periodic copies of all text on screen, save it to an array and call setText() every time the Undo method is called. This makes for a reliable system, but it isn't ideal for large (i.e. 1,000words+) text editing apps. This is because it:
Is wasteful. It could be that only one word changes in a two thousand word document, so that's one thousand, nine hundred and ninety nine words needlessly committed to memory.
Can lead to performance issues, as some low-tier hardware struggles with rendering large amounts of text. On some of my test devices, this method can lead to freezes of a few seconds whenever Undo is called.
The solution I currently use is comparatively complex, but I've published the results in a library here.
Essentially, this library saves a copy of text as soon as a user begins typing, and then another copy of text once they've stopped typing for a set amount of time (in my case, two seconds). The two text strings are then compared, and the altered section of text returned, the indexes where the alterations occured, and details on whether or not the change was an addition of new text, a deletion, or a replacement of old text with new text.
The net result is that only the necessary text is saved, and when Undo is called, there is only a local delete(), replace() or insert() call, which makes for much faster operations on large text fields.
Here is the undo/redo implementation that was linked to from Gary Phillips' answer extracted into a reusable and universal undo/redo plugin for any widget that descends from a TextView. I added some code for persisting the undo history.
http://code.google.com/p/android/issues/detail?id=6458#c123
Hope this helps.
To preserve EditText Styling with regards to undo:
You can create an ArrayList<EditText> or ArrayList<String> (String containing html text) to store your last 10 (for example) actions. So ArrayList [0] would contain html text from your first action and ArrayList [9] would contain html text from your very last action. Each time the user taps "undo" in your app, you would apply ArrayList [size()-1] to your EditText and then remove ArrayList [size()-1] from your Array.

android -- wait for user input

I'm creating a spades app with 1 human player and 3 computer players.
The problem that I am having is, the play must happen sequentially (clockwise) and I need my program to wait on the player input. I can't use wait() and notify(). I have tried while loops to check whether the user has selected a card but those stop the program from running. I've tried recursive methods that won't return anything until the player has chosen a card. That too does not work. So what do I do? I'm stuck.
My method goes like this (leaving out the non-pertinent code)
private void game(){
while(there are more tricks to be played)
while(each player has not played){
if(human turn)
get input from player
else
computer plays
}
}
Maybe you should change a little bit your game controller. Instead of waiting for anything, have your program continuously paint the screen. If user inputs nothing, same screen is paint all the time.
Once he presses a key (or clicks a card or whatever events you have), simply modify the paint method's argument (the screen to be painted). Thus you will separate painting the screen and input handling. It's a common technique called MVC model.
Maybe this will help (it's my game creating blog, and the links inside it are also helpful):
http://m3ph1st0s.blogspot.ro/2012/12/create-games-with-libgdx-library-in.html
You don't need to adapt all your game to the game described there, only the technique of separating input from painting. As a general technique, you should NOT have input determine painting.
Hope it helps. Don't hesitate to request further help.
You can try to add Event Handlers. It will try triger a event every time the user selects a card.
Try this.
Create one thread and in that threat call sleep(1000);

Categories

Resources