Search code examples
androidandroid-webview

Submit form button does not work on WebView


When loading some websites in a WebView, I am not able to submit their forms. When clicking the submit input (button), it gets focused, but nothing else happens.

Those same sites work on Chrome (in the same device).

How can I fix this? Is there a setting I am missing?

Feedback on why this issue happens will be appreciated, too.


Example

Steps to reproduce

  1. Load BrowseActivity, which will load https://m.ebay.com/
  2. Once the page loads, enter a search query (e.g. "Laptop")
  3. Press the form's blue search button or the action button of the software keyboard (magnifier glass icon)

Expected behavior: The search form should be submitted and the search results shown

Actual behaviour: The blue search button gets focus (represented by a bright border), but nothing else happens.


BrowseActivity.java

public class BrowseActivity extends AppCompatActivity {

    WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse);

        mWebView = (WebView) findViewById(R.id.home_wv_content);
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.getSettings().setJavaScriptEnabled(true);

        // Ebay has a blue search button where this issue can be replicated
        mWebView.loadUrl("https://m.ebay.com");
    }
}

activity_browse.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="my.example.BrowseActivity">

    <WebView
        android:id="@+id/home_wv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

Edit:

I have considered using a custom JavaScript interface in order to catch the click on the button, read the input query and then redirect to the search page.

Anyway, this approach is tightly coupled to the website itself (e.g. ebay) and I'd like this to work with different sites. Also, I don't want changes in the website to break my application.


Solution

  • You may need to enable other things in webview to have full equivalent of a browser. There are other things that are disabled by default in webview in addition to javascript).

    Try this setting (based on my project where we did have some problems with webview):

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setDatabaseEnabled(true);
    mWebView.getSettings().setDatabasePath(dbpath); //check the documentation for info about dbpath
    mWebView.getSettings().setMinimumFontSize(1);
    mWebView.getSettings().setMinimumLogicalFontSize(1);