Why is Android WebView refusing user input?
Asked Answered
G

18

38

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?

Gyno answered 18/1, 2010 at 4:31 Comment(0)
G
45

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.

Gyno answered 18/1, 2010 at 23:19 Comment(2)
Didn't work for me.. I dont think it is issue with focus, because, soft keyboard comes up on tap, also, cursor is blinking in the text field. (Issue is with facebook login page). Pls advice.Polymyxin
Just came across this issue myself and none of the solutions presented in this question were working for me because I was using a WebViewFragment and setting the properties with getWebView(). What worked for me was to sub-class WebViewFragment, override onCreateView and call requestFocus(View.FOCUS.DOWN) from there.Bianco
L
16

@Mac Does this single line :

webView.requestFocus(View.FOCUS_DOWN);

solved your problem ? It 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.

Linebreeding answered 20/10, 2010 at 5:55 Comment(2)
Excellent. Saved me many more hours after already having spent a few! Thank you.Vibrator
I also had to request focus in the touch handlerBircher
A
15

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

});
Abject answered 19/11, 2013 at 15:36 Comment(2)
Alternative 2 worked for me. Not sure why the view wasn't getting the focus for the first two.Ralli
webview.requestFocus(View.FOCUS_DOWN); did the trick for meSonatina
S
9

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!

Subset answered 6/3, 2015 at 8:42 Comment(2)
You are an angel, did you know that? I have tried so many alternatives for days, and this simple solution is the only one that actually works perfectly. Life saver. ThanksSile
This IS THE BEST! Many tanks!Baran
E
6

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

Erde answered 29/2, 2012 at 14:17 Comment(1)
This is interesting and helps to explain the problem, but is not actually an answer. I can't (for example) force Facebook to change the layout of their login page just because it's causing my app to have issues...Gyno
S
5

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.

Syllabize answered 17/12, 2014 at 15:21 Comment(0)
N
2

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);
Narine answered 18/1, 2010 at 7:27 Comment(1)
Yes, I have enabled JavaScript. I don't imagine that should be the issue either, since AFAIK the forms don't rely on JS for the basic input. I've also tried using the Facebook WAP login site (which definately doesn't use JS) with the same results. Thanks though.Gyno
G
2

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

Gauffer answered 16/8, 2015 at 14:34 Comment(0)
R
2

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);
}
Ralli answered 27/4, 2017 at 14:53 Comment(0)
A
1

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.

Aulea answered 13/8, 2010 at 2:49 Comment(1)
When are you setting FOCUS_DOWN? in onPageFinished?Straighten
G
1

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.

Grider answered 16/7, 2013 at 19:39 Comment(0)
F
0

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>&nbsp;</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.

Floruit answered 18/6, 2013 at 22:16 Comment(0)
B
0

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!

Baldheaded answered 29/7, 2014 at 12:51 Comment(0)
F
0

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.

Foresheet answered 4/5, 2015 at 13:25 Comment(0)
S
0

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.

Spasmodic answered 21/7, 2021 at 16:35 Comment(0)
R
0

In my case, this problem was strangely related to the zoom controls.

I have a subclass of WebView and if I turned on the zoom controls in WebSettings with this

this.settings.builtInZoomControls = true

then I was unable to tap into form controls on web pages.

The solution for me was to either set the above property to false or to also turn off the display zoom controls:

this.settings.displayZoomControls = false
Rhinarium answered 3/6, 2024 at 19:12 Comment(0)
B
-1

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

Baroja answered 16/4, 2015 at 16:49 Comment(0)
H
-1

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!

Horsey answered 3/7, 2015 at 9:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.