1

I am trying to make a YouTube link on my app to open in an external window/tab. Can anyone help me?

Here is my code :

import com.actionbarsherlock.app.SherlockFragment;

public class Showing_now extends SherlockFragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View mainView = (View) inflater.inflate(R.layout.activity_showing_now, container, false);
    WebView webView = (WebView) mainView.findViewById(R.id.webViewsn);

    webView.setWebViewClient(new MyWebViewClient());
    webView.getSettings().setPluginsEnabled(true);
    webView.getSettings().setBuiltInZoomControls(false); 
    webView.getSettings().setSupportZoom(false);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   
    webView.getSettings().setAllowFileAccess(true); 
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setPluginState(PluginState.ON);
    webView.loadUrl("http://www.myweburl.com");
    return mainView;
}
    private class MyWebViewClient extends WebViewClient {

    }
}
scunliffe
  • 62,582
  • 25
  • 126
  • 161
Ahmed Almahmeed
  • 97
  • 2
  • 15

2 Answers2

1

If I understand correctly, you want to open YouTube links in the YouTube app... If you want all YouTube links to open in the app, use this:

public class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.startsWith("http://m.youtube.com")||url.startsWith("http://www.youtube.com") {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } 
        return false;
    }  

@Ty221's answer will work also, but this answer will automatically try to open all YouTube links in the YouTube app.

anthonycr
  • 4,146
  • 1
  • 28
  • 35
0

If you want to watch that video in external application, use that function :

public static void watchYoutubeVideo(String id){
    try{
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
         startActivity(intent);                 
         }catch (ActivityNotFoundException ex){
             Intent intent=new Intent(Intent.ACTION_VIEW, 
             Uri.parse("http://www.youtube.com/watch?v="+id));
             startActivity(intent);
         }
}

If YouTube application is installed video will be shown in it, else browser will be opened .

TN888
  • 7,659
  • 9
  • 48
  • 84