Problems aligning a Youtube clip centrally in Android WebView

I am trying to show a YouTube clip embedded in a WebView in Android, with a web view size up to 640x360 pixels and in the center of the tablet screen. I managed to do this, however the YouTube clip seems to be disabled and a white background is displayed as shown in the screenshot below:

screenshot

Here is the Java code:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class VideoWebViewActivity extends Activity {


    private static final int VIDEO_WIDTH = 640; 
    private static final int VIDEO_HEIGHT = 360;
    private WebView webView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        webView = (WebView) findViewById(R.id.weblink_webview);

        webView.getSettings().setPluginsEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient());

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(final WebView view, final int errorCode, final String description, final String failingUrl) {
            }

            @Override
            public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);

            }

            @Override
            public void onPageFinished(WebView view, String url) {

            }
        });

        String youtubeUrl = "http://www.youtube.com/embed/O1N2rENXq_Y";

        String embedCode = "<!DOCTYPE html><html><head>" + " <style type=\"text/css\"> body { background-color: transparent; padding:0; margin:0; }</style>"
                        + "</head><body><div align=\"center\"><iframe align=\"middle\" width=\"" + (VIDEO_WIDTH) + "\" height=\""
                        + (VIDEO_HEIGHT) + "\" src=\"" + youtubeUrl
                        + "\" frameborder=\"0\" allowfullscreen></iframe></div></body></html>";


        webView.loadData(embedCode, "text/html", null);
    }

    @Override
    public void onDestroy() {
        if (webView != null) {
            webView.destroy();
        }
        super.onDestroy();
    }
}

And here is the xml layout:

<?xml version="1.0" encoding="UTF-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/weblink"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:visibility="visible" >


    <WebView
        android:id="@+id/weblink_webview"
        android:layout_width="640dip"
        android:layout_height="wrap_content"
         />

</RelativeLayout>

Edit: The strangest thing about this is that it works great on a 7-inch tablet with Android 3.2, but on a 10-inch tablet with 4.0.3 (ICS) it looks like a screenshot. 10 inch tablet with a honeycomb 3.1. This makes me think that there is some parameter / problem causing the behavior in the web view itself.

2: , , -, , -. , 100dip, 100dip .

.

,

+3
1

, :

- ICS , google WebView .

,

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" />

ICS .

0

All Articles