how to play youtube videos in android programmatically

Posted in Android By Raj On February 5, 2017


In this article, I am going to cover how to read most popular videos from the youtube and play in android application, you need to use youtube data API  to get list of popular videos in the JSON format, please check YoutTube data API  to get more information

In order to communicate our android app to youtube, we must generate signed key using google developers dashboard, for this, you need to login to developers console and create a new project called “android-youtube-player-api”

In the dashboard on the left side panel you will find Library link, on clicking of the Library link it will list out all the Google API’s in the main panel

Check for YouTube APIs section in the main panel then click on “YouTube Data API” and click on Enable link

You need to have credentials to access youtube data API so, click on create credentials button and select “where will you be calling the API from to “Android” and what data you will be accessing to “public data” and click on what credentials button to generate public key and finally click on done

The generated key is unrestricted one and we should not use this for production deployment, you need to generate new key for production release by selecting android selection in the Key restriction section but, here I am using public keyword which was generated recently



Creating Android Project

Open Android Studio,  create a new project and enter application name is “YouTubeVideoList” fill all mandatory fields once the project is created then open build.gradle file and add below entries

Open androidmanifest.xml file and add network permissions entry


Generate POJO’s

We can use jsonschema2pojo tool to generate POJO’s based on the provided JSON array or object, you just need to hit the youtube videos API and get the JSON array and then paste it into jsonschema2pojo dashboard text area field to generate POJO’s, once POJO’s are generated copy and paste into models folder of package, refer the below screen shot



I am going to use android retrofit library to communicate with youtube rest api’s, so for that first, I am going to create new interface called YouTubeInteface and define a method called getVideos(), by calling this method using retrofit client it makes a call to the youtube API and fetch most popular videos data

The below class is going to be used to instantiate YouTube retrofit client in the MainActivity class

Create VideoAdapter class and assign videoid’s to the thumbnailloaders

In the main activity, oncreate method make a request to the youtube api by using retrofit and assign the adapter to the listview

Activity_main.xml file



  • Abid

    what is this…
    there is a a lot of errors


  • Jimmy

    hello,only list is display no action found after click on list item video

    • Aalex Headen

      Add click listener to relative layout or image view then it will work and you can watch the video.

  • Jimmy

    what is BASE_URL?