Search code examples
android-intentyoutubeandroid-webviewandroid-2.3-gingerbread

Android 2.2/2.3 doesn't load YouTube videos on the YouTube app from a WebView


I know that this question has been answered many times, but I still can't get it to work on Android 2.2 nor 2.3.

I am developing an app which is composed of tabs. One of those tabs loads the mobile version of YouTube inside a WebView. So far so good.

The problem is that the videos don't play inside a WebView, so I'm using the YouTube app as here is suggested. Great! It works in Android 2.1, but not in 2.3. All the phones that I'm using have the YouTube app instaled. It simply doesn't display the window that let the user choose the YouTube App or the browser. Any suggestion? Here is my onCreate method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.website);
    this.webView = (WebView) findViewById(R.id.webview);
    this.webView.setWebViewClient(new WebViewClient() {
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
             // YouTube video link
             if (url.startsWith("vnd.youtube:")) {
                 int n = url.indexOf("?");
                 if (n > 0) {
                     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("http://www.youtube.com/v/%s", url.substring("vnd.youtube:".length(),n)))));
                 }

                 return true;
             }

             return false;
         }
    });

    WebSettings webSettings = this.webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    this.webView.loadUrl("http://m.youtube.com/");
}

Thanks in advance.


Solution

  • It's like the Twitter problem, and I solved it in a dirty way.