Android 4.4 WebView issue with requestFocusFromTouch - android

I am seeing some unexpected behavior when using a WebView in Android 4.4 (build target 18). One one page in particular we have some edit text fields, and to get the soft keyboard to pop up appropriately, we had to use a code snippet similar to the ones described here:
https://code.google.com/p/android/issues/detail?id=7189
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
This worked as expected up through version 4.3, but beginning with 4.4 the code above caused an undesired effect of having the content of the webview snap/scroll back to the top of the page on the completion of a touch - after scrolling down the page.
Has anyone else experienced this new behavior - or figured out a workaround? The only thing I have come up with so far would be to subclass a webview that allows the edits (so the code above can be removed from read-only pages that now have the scrolling issue). Of course, if a page both scrolls and has edit fields, this would not work.
Thanks in advance!

Related

How to stop Touch To Explore feature of TalkBack in my app

I'm working on a project for blind people, there're a lot of troubles I need to fix if the user activates TalkBalk on his phone.
I'm creating a soft keyboard for blind people, the blind will tap with single finger on the circles "Braille Cell Dots" to generate a Braille code, then he types the character/number/symbols he wants as they presented in Braille language.
My problem now is Touch To Explore feature of TalkBack, the user will not be able to make a single tap on the screen because this action now handled by TalkBack, the user must double tap on the dot and this is not good for my app!
How to generate a single tap even if Touch to Explore feature is enabled in TalkBack?
Based on this answer which has solved my problem, as the single touch event converted to hover event if touch to explore is enabled, I've added onHoverEvent to call onTouchEvent and works fine:
#Override
public boolean onHoverEvent(MotionEvent event) {
//Move AccessibilityManager object to the constructor
AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
if (am.isTouchExplorationEnabled()) {
return onTouchEvent(event);
} else {
return super.onHoverEvent(event);
}
}
And handle hover action to onTouchEvent:
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_HOVER_ENTER:
//Your Code
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_HOVER_MOVE:
//Your Code
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_HOVER_EXIT:
//Your Code
break;
}
return true;
}
I'll edit my question to be more cleaner :)
You don't. It's a terrible idea. Come up with gestures and mechanisms that fit within what TalkBack allows. If you could annotate a specific feature or mechanism of your app that is not allowed to work with talkback, I could recommend an alternative. What gesture is it that's not working?

Missing focus cursor on input field hybrid android cordova webview

We have android hybrid app with Cordova where are creating custom inappbrowser with cordova webview to show the content. In Android lollipop devices, when user selects input field, keyboard pops up and user can enter test but there is no curser in the input field. This is not happening in pre lollipop. I tried below options, also this happens sporadically, i am getting focus sometimes but most times it is not working
inAppWebView.getSettings().setJavaScriptEnabled(true);
inAppWebView.requestFocus(View.FOCUSABLES_TOUCH_MODE);
or
inAppWebView.requestFocus(View.FOCUSABLES_ALL);
or
inAppWebView.requestFocus(View.FOCUS_DOWN);
or
inAppWebView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
Any suggestions?
Though now probably seomewhat outdated, the solution can be found at https://issues.apache.org/jira/browse/CB-11248
Basically, you need to change onPageFinished to
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.clearFocus();
view.requestFocus();
}
After quite some thinking and effort looking through the repo history, we finally nailed it down & fixed it.
While initializing a plugin we did perform some javascript on the WebView. We we're using 'evaluateJavascript' to perform the javascript (this does work on Lollipop, not on KitKat btw), but that somehow had the side-effect of making the caret disappear. After using 'loadUrl' instead, it did work again.
In the javascript we did add a custom attribute (HD_APP) to the window, so nothing that should cause such a side effect. As you can see in the screenshot, the input was no longer identified with ":focus" by css.
Hope this helps..
The thing that caused the issue for me is that I called evaluateJavascript before I had any page loaded.
After I made sure that my page was ready for JS before sending any, the issue disappeared.

Using MotionEventCompat in android

I'm looking for an example of how to use MotionEventCompat in Android. I'm using API level 10, which doesn't support if a finger is 'hovering' or 'dragging' onto a view. I need to detect this, preferably from the view itself. Here's some code snippets regarding how I'm trying to use this:
**my class:**
import android.support.v4.view.MotionEventCompat;
public class GridButton extends View
overriding onTouchEvent:
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction() & MotionEventCompat.ACTION_MASK) {
case (MotionEvent.ACTION_DOWN): {
set_active(true);
return true;
}
case (MotionEventCompat.ACTION_HOVER_ENTER): {
set_active(true);
break;
}
}
return false;
}
I based the MotionEventCompat.ACTION_MASK off an example I found somewhere, but it doesn't trigger my code for set_active().
Any help on using this would be appreciated. There's very little about this on the web.
Hover events are sent when the device supports a mouse or touchpad. When the cursor hovers over a view these events are sent to onGenericMotionEvent, not onTouchEvent. They won't help you detect a finger that isn't touching the surface of a capacitive touchscreen or a finger that touched down in a different position and then slid over the view in question. They will never be sent on an API 10 (Android 2.3) device.

Android: ScrollView 'setOnScrollListener' (like ListView)

I want to do something when the user has scrolled >90% down, so I thought I could just add a onScrollListener like I would in a ListView. Unfortunatly, ScrollView doesn't seem to have a similar method. Is there any way I can do what I want; getting a notification when the user scrolls approx 90% down?
Thanks,
Erik
This is what I end up doing to know if the user was interacting with my ScrollView or not:
findViewById(R.id.scrollview).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_SCROLL:
case MotionEvent.ACTION_MOVE:
setScrollState(OnScrollListener.SCROLL_STATE_FLING);
break;
case MotionEvent.ACTION_DOWN:
setScrollState(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
setScrollState(OnScrollListener.SCROLL_STATE_IDLE);
break;
}
return false;
}
});
You can try extending ScrollView and overriding View#onScrollChanged and the doing your checks there. You have to extend ScrollView since onScrollChanged is a protected method.
You can implement an onTouchListener and use the getScrollX and getScrollY callbacks of the view
If you want a scrolling view that works like a listview, may I suggest trying out a horizontal listView library:
https://github.com/sephiroth74/HorizontalVariableListView
it's based on the original ListView, and it works very well.
it has its OnScrollListener , and even though the website says it supports Android 2.3 and above, I think it should work from API 7 and above (and with some tweaks even older).

Why is Android WebView refusing user input?

I'm developing an Android application that uses a WebView to display the login page for Facebook. The page loads beautifully, and I'm able to select the username/password textboxes, but typing in them will not work. That is, they definitely have input focus (they have the orange focus highlight box and a flashing cursor), but typing in them does absolutely nothing. I'm not certain, but I think maybe the form buttons are also playing up - they appear to be simply refreshing the page, rather than submitting the form.
Just to be clear, although I'm particularly interested in getting Facebook running, I'm sure that this isn't a Facebook issue since other websites (Google, etc) also display the same behavior.
Does anyone have any ideas what might be the issue?
Turns out that it was apparently the WebView not having focus that was the issue.
I discovered that using the arrow keys to get focus on the textboxes caused them to work, so I theorised that there was an issue somewhere with something not having focus, most likely the WebView not having focus. Sure enough, adding the following line seemed to fix the problem:
webView.requestFocus(View.FOCUS_DOWN);
I'm still at a loss to explain exactly why the issue occurred in the first place - the textboxes should work whether they receive focus from being tapped upon or through being "arrowed" to - but at least I have a solution that appears to work.
Thanks for your input wf.
#Mac Does this single line :
webView.requestFocus(View.FOCUS_DOWN);
solved your problem ? I didn't in my case ,but this does :
mWebView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
just for your referrence.
These are most have
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.requestFocus(View.FOCUS_DOWN);
and if still not working then use one alternative below
Alternative 1
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
Alternative 2
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
v.requestFocusFromTouch();
break;
}
return false;
}
});
I don't know my case is exactly same as yours,
I found that add one css line can solve problem.
I just add
input{
-webkit-user-select: text;
}
to my css, then problem solved!
I'm sorry to say that none of this worked for me. I guess this depends on your context. In my case, the problem occurred within a html pop up (absolute div). All the other input field were doing ok.
Breaking down the problem I found out it's a bug in the webview. Fixed positioned divs containing other fixed/absolute position divs break input fields in the page. Hence a rule I worded for you (don't hesitate to reformulate):
If in your page you've got a fixed positioned div with none-default positioned nested divs (i.e. absolute, fixed, etc.), any of the following absolute positioned div in your page containing input fields will get the unexpected behaviour.
So, put your input fields at the top of the page (if you can) or avoid absolute divs nested in other fixed/absolute divs. That should help.
I posted something about it on my blog if you need more explanations and potential workarounds: http://java-cerise.blogspot.com/2012/02/android-web-view-inputfields-refuse-to.html
In my case TabHost from a previous fragment in back stack was stealing the focus from WebView input on every key press. Here's how I fixed it:
tabHost.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
#Override
public void onViewDetachedFromWindow(View v) {}
#Override
public void onViewAttachedToWindow(View v) {
tabHost.getViewTreeObserver().removeOnTouchModeChangeListener(tabHost);
}
});
See https://code.google.com/p/android/issues/detail?id=2516 for more on the topic.
Not sure if it's the problem since other websites also display the same behavior, but have you enabled javascript? A WebView does not enable it by default.
WebView webView = (WebView)findViewById(R.id.yourWebView);
webView.getSettings().setJavaScriptEnabled(true);
I spend a lot of time to solve this problem. Finally, I realized that it is not about WebView. In my case, I have a webview inside dialog. I want to handle back pressed button to cancel dialog and finish activity as well. And I wrote code below:
private void setOnBackPressed() {
this.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialog.dismiss();
activity.finish();
}
return true;
}
});
}
So, when I use the keyboard, this method reject all key presses somehow and then it doesn't appear on the text fields.
I only changed the return value to false. So, it worked properly.
Hope it helps!!
PS: this method is inside my dialog class that extends Dialog class
I tried all the other solutions posted here, none of which worked.
Instead, I extended the WebView and overrode the InputConnection which forced KeyEvents to dispatch.
#Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new BaseInputConnection(this, false);
}
I encountered this problem because I am displaying a splash image while the webview is loading the first page. I am setting the webview to View.VISIBLE in onPageFinished, however despite being able to focus text boxes you can't type into them. I too can confirm that the fix to set the webview requestFocus to View.FOCUS_DOWN works.
NB. If I set the webview to View.VISIBLE in onResume the problem does not present itself but in my case the splash image disapears too soon.
It was happened to me loading a page in 4.1.2 and 4.2.2 and after one day of searching, I found the answer here (comment #18)
Quote from the original post:
Some of the various web pages I was rendering with WebView didn't fit properly into the WebView and as a result a div (or some other html component ) were being invisibly laid over the input fields. Although the input fields appeared selected when touched, they would not allow text input (even if i did manage to get the soft keyboard up using the track ball).
So the solution.
webview.getSettings().setUseWideViewPort(true);
This won't completely stop the issue, but makes the view more like a PC browser which sites are designed for. Less change of the overlay.
I found a problem that might be different, but it sounds similar. In the android 2.3 native browser, if you have fixed position elements on the page, it sometimes breaks select boxes.
A workaround for this problem for 2.3 devices is to never allow empty child elements for a fixed position parent. Thus,
<div style="position:fixed">
<div>
<span>not empty</span>
<span></span>
</div>
</div>
would become
<div style="position:fixed">
<div>
<span>not empty</span>
<span> </span>
</div>
</div>
This fixed my problem.
Don't know if this was the issue for your problem, and this is over a year after-the-fact, but figured I would share, as I'm currently dealing with another fixed postion issue on an app, and I haven't found a workaround for it.
I faced with similar problem, that text inputs doesn't work on webview. When you focus on text input it shows keyboard, but you cannot type any characters.
After a long search for solution, I found that this fixes the problem:
body {
-webkit-transform: translate3d(0,0,0);
-moz-transform: translate3d(0,0,0);
-ms-transform: translate3d(0,0,0);
-o-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
}
As I understood, this rule makes some devices run their hardware acceleration.
More information about translate3d(0,0,0) here.
On the other hand, according to this article it is not recommended to use GPU acceleration everywhere.
in my context (webview containing an external URL), this fragment works:
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
v.requestFocusFromTouch();
break;
}
return false;
}
});
the error is because at every touch the webview was losing the focus!
By default, In Android, Webview allow user input with basic configurations:-
android:focusable="true"
android:focusableInTouchMode="true"
& enabled JavaScript but make sure there is not any activity/fragment level focusability blockage, in my case
android:descendantFocusability="blocksDescendants"
just removed this, worked for me. So cross-check such cases & fix them accordingly.
If we are dealing with EditText in WebView, then v.requestFocusFromTouch () will not work if we have to USER_AGENT written Mozilla / 5.0 (X11; Linux i686) and will work fine if Mozilla / 5.0 (X11; Linux x86_64).
It also happened in my project. I tried all above methods, none of them can effective. Finally, I solved this problem by use a dialog theme activity replaced the native Dialog. Hope this can help someone!

Categories

Resources