2

I have list of tweets being displayed in my listview. I have setup a listener on my listview which should take the user to the specific tweet.

The listener on my list is as follow:

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(TweetActivity.this, WebActivity.class);

            intent.putExtra("url", String.valueOf(twitterBaseURL+tweetResult.get(i).getId()));
            startActivity(intent);
        }
    });

Inside my web activity class, I am doing something as follow:

String url = getIntent().getExtras().getString("url");
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl(url);

In the web view it does not load the page like it does in google chrome on a mobile. I think the javascript does not load properly.

UPDATE 1:

I was testing this on an android device with API 21. Inside my android studio I have a virtual device with an API 23. In API 23 this loads everything properly. I created another virtual device with API 21 and tested to double check so the problem is with the API levels. This code works in API 23 to load the url properly inside the web view but does not in API 21?

indexOutOfBounds
  • 541
  • 7
  • 27

2 Answers2

0

Enabling JavaScript

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView. You can retrieve WebSettings with getSettings(), then enable JavaScript with setJavaScriptEnabled().

For example:

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_assets/your_page.html");
0

Take a look at this, although the issue talking about is react-native.

The last point mentioned there is about try running on a real Android device. And indeed sometimes Android emulator does not work as expected.

So I suggest testing your code on the real android device with API 21 first

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
  • Thank you for your reply. That's what I am saying, neither does it work on the emulator and nor on the real device with API 21. Other than API 21 it works fine – indexOutOfBounds May 30 '18 at 15:35