How to implement Youtube Player Fragment in Android App

How to implement Youtube Player Fragment in Android App

In previous article we learnt how to play a youtube video in android using YoutubePlayerView and using YoutubeBaseActivity.

But there is one problem in YoutubePlayerView that we need to extend our activity with YoutubeBaseActivity. What if we don’t want to extend our activity with YoutubeBaseActivity or we want to implement the YoutubePlayerView in fragment.

There is a solution for the above problem i.e YoutubePlayerFragment. A fragment that contains a YouTubePlayerView. Using this fragment is the preferred way of playing YouTube videos because your activity does not need to extend an activity provided by the library, as is the case with using the YouTubePlayerView directly.

So today we are going to work on the same only and we will see how to implement the YoutubePlayerFragment and play our video.

Before starting this article please go through to the previous article about YoutubePlayer and do the below mentioned tasks:

1.  Create the project in google console and get the Developer key.

2. Add then download the YoutubePlayerAPI jar file and add into your project.


1. Create a new project in Android Studio by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is

2. Open res ⇒ values ⇒ strings.xml and add below string values. These are some strings that we are going to use in our project.

3. So before getting started you need to download the jar file of Youtube Android Player API. Download the jar file and add into your project dependency. After adding jar to dependency  the gradle will look like below:

4. Now open your activity_main.xml and add the below code to it. In this class add the as fragment and one RecyclerView to display the list of videos in horizontal direction.

5. Now open your and add the below code to it. In this class we need to find the id of YoutubePlayerFragment and initialise the YoutubePlayer.

6. For RecyclerView item row create new xml layout naming youtube_video_custom_layout.xml and add the YoutubeThumbnailView to it inside CardView.

7. Now create adapter for RecyclerView naming and add the below code to it.

Youtube Player Fragment

Youtube Player Fragment

8. Don’t forget to give Internet permission in your AndroidManifest.xml file else your app will not run.

9. Finally all done, now you can also play video in your app without using YoutubeBaseActivity.

Thanks. :)

Download Source Code


About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.


I added my own api key and try to run this code but getting error like “An error occurred while initializing the youtube player”

    Hi Archana

    Can you tell me does your project at Google console has YouTube Data API enbaled?

    Does your manifest has INTERNET permission?



It works with No problems!!!


I have been struggling to use this app on a android tv box, but I am unable to click or play any of the videos with the remote. It only works on a phone. I have tried many ways but it doesn’t seem to work. Please help.

    Hi Emil,

    I don’t have any idea about TV Box as i haven’t tried it.

    Actually the app is only for mobile so i think it wont work on TV.


Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)