Keyboard in HTML text-input disappear when WebView call 'loadUrl' again - android

I use WebView for my Androind App. I got a problem and request a solution for help.
There is a textfield in the HTML page. When it gets 'focus' and then I call
mWebView.setFocusableInTouchMode(true);
in Java code so that the Android soft-keyboard will pop-up to let me key in.
The problem is I need using multi-thread for some processes in Java and call
mWebView.loadUrl(strJSCall);
as callback to execute JavaScript function, but the keyboard gets hidden!
The way I try is to force the keyboard to show again. But how can the keyboard always show when 'loadUrl' is called?
Dose anyone meet the same issue and solve it already?
Sincerely,
Jr.

The problem with loadUrl is that it interrupts the UI thread and will still close the input method. The only solid way to do this is to check what the cursor is on the webview, and override the default loadUrl behaviour on your webview.
Inside your custom webview:
#Override
public void loadUrl(String url) {
HitTestResult testResult = this.getHitTestResult();
if (url.startsWith("javascript:)" && testResult != null && testResult.getType() == HitTestResult.EDIT_TEXT_TYPE)
{
//Don't do anything right now, we have an active cursor on the EDIT TEXT
//This should be Input Method Independent
}
else
{
super.loadUrl(url);
}
}
This will prevent the native side from loading Javascript from firing when you have focus on a text field in webkit. It's not perfect but it avoids the mess of trying to figure out whether your text field is visible either by the resizing of the WebView. The Javascript executing in the webview should still work fine.
Again, if you need something to update while you're typing, you may have to find another way for Java to communicate with Javascript that doesn't block the UI thread. This is a hard problem that I still haven't solved properly yet.

I have the same problem and haven't fully solved it yet.
I have been able to force the keyboard to show after calling webView.loadUrl, first you need to find out if the keyboard is showing, I used a modified version of this How to check visibility of software keyboard in Android?
then call
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(webview, InputMethodManager.SHOW_IMPLICIT);
But if like me you need to communicate with the JavaScript in the WebView after each key press then this approach causes the keyboard to flicker as it tries to animate up and down. It also misses some of the input depending on how fast you type.

In Android, executing javascript from WebView is one-way: calling WebView.loadUrl("javascript:sub(1,1)"); You can not get the return value directly.
There's a async way to get the JavaScript function return value:
How to get return value from javascript in webview of android?
But in most cases, we need a sync method instead of the async way. For example, while click event is passed into Android WebView's onTouch
method, we want to execute a JavaScript to reading the element information clicked on.
I have tried two solutions:
1. Use Java P/V variable to wrap a sync method from the async on, the solution looks like:
How to get return value from javascript in webview of android?
The solution is totally not working, because wait method will block the execution of JavaScript and JavaScriptInterface. So if we want to get the result of a js
method in a UI Thread. it's not possible.
Use Java Reflection to get the Native Interface of stringByEvaluatingJavaScriptFromString.
Exist stringByEvaluatingJavaScriptFromString for Android
Works very well, the only thing need to care is to initial reflection method when page loaded finished. I tried to initial the reflection inside constructor of WebView, but failed. I think the constructor is async method.

Related

does react-native have autoCompleteDropdown?

how is it possible to implement a native autocompletetextview for react native with java?
how come you don't have any?
You can make you custom component for the same, by calling the api on changetext of text input and on modal you have a flatlist to render the response for the same. But then in this case you have to cancel the fetch call of the previous state if user enters the text very fast otherwise you will end up calling the server many times.
Alternatively you can use the libaries below:
1. http://github.com/mrlaessig/react-native-autocomplete-input
2. https://github.com/maxkordiyak/react-native-dropdown-autocomplete
I hope this helps...Thanks :)

Prevent android webview soft keyboard to lost focus

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

Issue with webview

In my application i use a mix of html and native. In my web view i load a html page which has a which has an image in it. On click of the image i call a java script function which in turn calls the native code. My html tag is as below:
<img onclick=nextLevel('0'); id="d2">
My javascript method is as below:
function nextLevel(index)
{
Android.displayNextLevel(index);
}
Within the displayNextLevel method i start the next activity. The issue is when i click on the image on the html page multiple times the event gets triggered multiple times and the activity opens up multiple times. Am i missing out on something? How do i overcome this issue? Kindly help me with this. Thanks in advance.
I guess you could fiddle around in the html/js to make sure you can only click it once every now and then.
You could also ditch your function and instead use the JavascriptInterface so you can handle stuff in your Java code instead.

How to completely reset Android webview before load new request

I am using Android WebView to load some webpages. In my case, I have to insert some JavaScript codes before loaded webpages. Just like below:
//enable javascript
mWebView.getSettings().setJavaScriptEnabled(true);
//inject my js first.
//I can't inject the js onPageStarted() or onPageFinished() because I need to make sure the js
//is injected before html loaded.
mWebView.loadUrl("javascript:MyJsCode");
//load HTML
mWebView.loadUrl("http://example.com/demos/index.html");
The code works fine first time, but failed when run it more than one time. Because the HTML can't find my JS.
I think because the previous HTML is not clear completely, so mWebView.loadUrl("javascript:MyJsCode") inject MyJsCode to previous HTML instead of the new HTML.
So I thought if I could completely reset the WebView(to clear the previous HTML), will solve my issue.
I tried WebView.ClearView(), loadUrl("about:blank"), they all doesn't work.
Anyone suggestion?
Though this is a a tiny bit late, I offer the following. Perhaps it may be helpful . . .
Your question does not specifically mention using any of the callbacks in the WebChromeClient, but you do mention JS, so the following may help. In SDK level 16 and below, you can use the callbacks without specifically "clearing" them. However, starting with SDK level 17, I observe that you must act to clear the events -- specifically alert(), which, of course, results in onJSAlert() being fired in your WebChromeClient if you override it. In all the devices I have tested at SDK level 16 and below, you may blithely ignore the callback, and all will go to plan.
However, you will note that onJSAlert, when overridden, delivers a JSResult object in the last parameter, thus:
boolean onJsAlert(WebView view, String url, String message, JsResult result)
I observe that the JsResult object has two methods exposed, thus:
Public Methods
final void cancel()
Handle the result if the user cancelled the dialog.
final void confirm()
Handle a confirmation response from the user
Assuming that one returns true in the callback (indicating that the callback consumed the onJsAlert event), and assuming that you are using SDK 16 or before, then WebView.destroy() will do the expected things.
I observe, however, that SDK 17 (4.2.x) seems to want some further proof that the callback did, in face, handle the event. Failing to call result.cancel() or result.confirm(), will leave your WebView (or, more to the point, the WebViewCore) stuck permanently. Nothing I have tried will reawaken the WebViewCore, and, thereafter, nothing will load in any WebView, new or otherwise. (Attempted explanation: WebView is merely a wrapper class for WebViewProvider, which, in turn instruments a WebViewCore object. It's that last fellow, WebViewCore, that does all the work. Through wandering the source code, and through reflection, you can burrow your way into that object, if you are keen to do so. WebViewCore is a static, thus, there is exactly one WebViewCore for the whole of your application. Ergo, if your one-and-only WebViewCore gets stuck, no WebView in your application will work thereafter. Once it is stuck waiting, for example, for a JSResult method to be called, it will be stuck until the application is destroyed (i.e. even pausing/resuming the app has zero effect). Even calling destroy() on the WebViewCore directly, through access gained through reflection is ineffective. N.B. Calling destroy() on the WebView has the side-effect of calling destroy() on the WebViewCore but that, too, does nothing helpful).
So, the symptom is that
you create a WebView
some clever JS runs, perhaps calling alert()
you handle the alert() in your onJsAlert override method
you fail to call either result.confirm() or result.cancel()
you destroy the WebView
thereafter, no WebView in your application will load anything.
The good news is that if you are sure to "clear" the events in whatever callbacks you override by invoking the appropriate JsResult method, then the WebViewCore will not permanently stop, and your application will be happy.
I think there is no way to do this except re-new a WebView.

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

Categories

Resources