Search code examples
androidandroid-intentyoutube

How can I open a YouTube video link directly from an Android app?


I want to play a YouTube video on a click of a Button, but I am getting an action dialog window that I don't want to show. I want that the YouTube video link should open directly in the YouTube app on a Button click rather than showing this "choose action" window:

Enter image description here

How is it possible?

Code:

 playVideo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=bzSTpdcs-EI")));
        }
    });

Solution

  • You can set the package name of the YouTube application:

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=bzSTpdcs-EI"));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage("com.google.android.youtube");
    startActivity(intent)