Sam
Sam

Reputation: 1232

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.

Upvotes: 27

Views: 16933

Answers (3)

Fakhriddin Abdullaev
Fakhriddin Abdullaev

Reputation: 4910

You just need to set JavaScriptEnabled true like below:

webView.getSettings().setJavaScriptEnabled(true);

Upvotes: 0

alkathirikhalid
alkathirikhalid

Reputation: 897

I had similar issues one in particular is to upload a file.

Feedback on why this issue happens:-

The issue is that there are different OS Version and different Web Engine Versions. Since I would assume the web view is presenting a HTML5 page and it doesn't work well on that given device, you would probably need more than JS enabled.

Some webpages keep track on the number of submits and they need access to client/web storage.

WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true); // Done above
mWebSettings.setDomStorageEnabled(true); // Try
mWebSettings.setSupportZoom(false);
mWebSettings.setAllowFileAccess(true);
mWebSettings.setAllowContentAccess(true);

I can't help much without knowing at-least the Android OS version which will point to the Web engine version used.

Cheers.

Upvotes: 1

Josef Adamcik
Josef Adamcik

Reputation: 5780

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

Upvotes: 13

Related Questions