Question

I have a simple WebView-based activity that follows the Hello, WebView example to enable JavaScript and overload shouldOverrideUrlLoading().

This Activity works great when the user touches links on the webpage. However, if the user uses the trackball and clicks on a link, then the resulting page load shows a page with JavaScript disabled.

public class ViewUrl extends Activity {
    protected WebView view;

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

        final String url = "http://www.opentable.com/phone/reviews.aspx?rid=2947";

        view = (WebView) findViewById(R.id.widget1);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl( url );
        view.setWebViewClient( new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });
    }
}

The view_url.xml file is straightforward:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/widget1" />

</RelativeLayout>

Below are some screenshots showing both the correct and incorrect behavior.

Click using Touch (CORRECT):

Click using Touch (CORRECT) http://img.skitch.com/20090908-g2acnrpb1riuq11ys3p1u6fd4d.png

Click using Trackball (INCORRECT):

Click using Trackball (INCORRECT) http://img.skitch.com/20090908-my23yxakudwhheq875j2hcwg6h.png

Any ideas how I can fix the Trackball behavior to be the same as the touch behavior? Or at least enable JS when links are clicked via the trackball?

Was it helpful?

Solution

It appears that the "Hello, WebView" example is broken. On the suggestion of someone on the IRC channel, I removed the override of shouldOverrideUrlLoading() and everything seems to work swimmingly.

Revised working code:

public class ViewUrl extends Activity {
    protected WebView view;

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

        final String url = "http://www.opentable.com/phone/reviews.aspx?rid=2947";

        view = (WebView) findViewById(R.id.widget1);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl( url );
        view.setWebViewClient( new WebViewClient() ); // probably not necessary if you don't do anything else with the WebViewClient
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top