Posted in February 2012

Preventing Auto-Fill in Android WebView

Working with the Android WebView can be very frustrating. One thing that was driving me nuts was that as I tried to fill out forms in a WebView it would offer suggestions from things I had typed before. It proved pretty difficult to search for a solution to the problem.

The first thing I found was a suggestion to set auto-complete=”off” on the form and/or the element. Unfortunately, that didn’t work at all in the WebView. However, I did waste a lot of time testing that out.

I tried a lot of different searches:  “webkit form input autofill”, “webkit form input autocomplete”, “android WebView disable autocomplete” and other permutations I can’t even remember. I even found this unanswered StackOverflow question.

It turns out that it’s very simple to disable the feature, just difficult to find it! It’s as simple as:

     webview.getSettings().setSaveFormData(false);

That’s it!  Pesky problem solved.

Update:  Kamal on StackOverflow added the following two lines:

     WebView.getSettings().setSavePassword(false);
     WebView.clearFormData();

I haven’t tested those two changes but they do seem very likely to help!

Android and Number Inputs in Forms

I had a form with a single input box and a hidden submit button that I had been using for getting user input in a form on a WebView. The input was supposed to be a number so I wanted the number soft keyboard to come up when the field was selected. Simply changing the input type to number gave me the functionality I wanted. Unfortunately, once I did this, pressing enter no longer submitted the form.  It appears the numeric keyboard on Android doesn’t do a submit (at least, if the input type is number).

Google searches were pretty fruitless, though I did find this unanswered StackOverflow question.

I did confirm that the keyboard doesn’t send any keys whatsoever. I was at a bit of a loss as to how to fix this problem.

The solution I hit on, which may be sub-optimal but met the needs I had, was to add a text input field and position it off the screen. I then added an onfocus script that set the text input to disabled and submitted the form. This gave me the behavior I wanted and didn’t add extraneous fields to the form. One more Android bug tackled.

Here’s the code and a sample page:

function numberFix() {
    numfix.disabled = true;
    theform.submit();
}
<form id="theform"><input id="thetext" type="number" />
  <input id="numfix" style="margin-left: -999px;" onfocus="numberFix()" type="text" />
  <input style="height: 0px; width: 0px; border: none; padding: 0px;" tabindex="999" type="submit" />
</form>
Tagged , ,

Hello world!

Welcome aboard! Don’t expect a lot. I’m reserving this place for random thoughts, mostly about programming and things I’m interested in. I realized I have been hoarding knowledge over the years and I decided to share what I can whenever I think to. Don’t expect much and you won’t be disappointed!