Mr Kohn Doew
Mr Kohn Doew

Reputation: 277

WebView doesn't show twitter widget

I want to show a twitter widget via web view in my Android app. I generate a widget and customize it a little bit

<div style='padding-left: 100px;'><a class="twitter-timeline" 
href="http://twitter.com/%s" 
data-widget-id="<number>"
data-screen-name="%s"
data-chrome="nofooter noborders noheader transparent"
data-related="benward,endform">
<FONT COLOR="FFFFFF">Tweets by @%s</a>
<script>
    !function(d,s,id)
    {
        var js,fjs=d.getElementsByTagName(s)[0],
        p=/^http:/.test(d.location)?'http':'https';
        if(!d.getElementById(id))
        {
            js=d.createElement(s);
            js.id=id;
            js.src=p+"://platform.twitter.com/widgets.js";
            fjs.parentNode.insertBefore(js,fjs);
        }
    }(document,"script","twitter-wjs");</script>

I programmatically load it from assets:

  WebView webView = (WebView) view.findViewById(R.id.webview);
    webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setBackgroundColor(Color.BLACK);

    webView.getSettings().setDomStorageEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.e(IrisDelegate.TAG, description);
        }
    });

    try {
        StringBuilder builder = new StringBuilder();
        InputStream is = getActivity().getAssets().open("twitter.html");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
        String line = "";
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }

        String twitter = getArguments().getString(TWITTER);
        String data = String.format(builder.toString(), twitter, twitter, twitter);
        webView.loadData(data, "text/html", HTTP.UTF_8);

But unfortunatly it doesn;t work, I see only link to page of my twitter user. Do you know where is an issue?

Upvotes: 0

Views: 1641

Answers (2)

Nesatur
Nesatur

Reputation: 51

Add this to your html head tag.

<script type="text/javascript" src="https://platform.twitter.com/widgets.js"></script>

Helped me immediately!

Upvotes: 3

prashantwosti
prashantwosti

Reputation: 1010

I had a same issue with iframe..I had to write the whole iframe block in a string to make it work.

also try adding:

wv.getSettings().setAllowFileAccess(true);
wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

it may help.

Upvotes: 0

Related Questions