Youtube channel integration in Android using YoutubePlayerView API

Youtube is an awesome platform to share and watch videos. In this tutorial will learn about how to do Youtube channel integration in Android using Youtube Player API.  You can also check their official Youtube Android Player  website for  reference. However checking the docs and going through line by line is a time consuming task. In this tutorial we will learn about how to integrate Youtube channel integration by following simple steps.

1. Find Youtube Channel ID:  Go to any Youtube’s channel homepage and copy the URL of that channel. Suppose if we copy URL of Narendra Modi’s channel the URL will look like this. The last words after channel/ is channel id.

2. Generate SHA1 key: When you signup for Youtube player API on Google Developer console you need to generate SHA1 key. For this follow these steps.

  1. Go to your Android Project and click on Gradle icon.
  2. Click on YourProjectName(root) after that if nothing is showing click on refresh icon.
  3. Now go to Tasks -> android -> signingReport. You will find SHA1 key.
Youtube channel integration in Android

SHA1 Key in Android Studio

3. Get API Key for Youtube Data API v3: Go to  Google Developer Console  and click on select a project you can choose existing project or create a new project for Youtube integration.

3.2 Search for Youtube Data API v3 choose this API and enable it. Now you will navigate to project dashboard.

3.3 Click on Credentials Now create credentials and copy your API key and save it. You can restrict  key by choosing according to your usage and paste the SHA1 key that we generated in step 1 and save it. We are selecting none, however it is always recommended that to  use restriction.

4.Get Channel video list in JSON format: Now go to the Youtube Data API docs  and scroll down you will find Authorize and Execute button for sake of simplicity we  execute data in  Load in APIs Explorer and we navigate to another page.

4.2 There are many parameters but only part parameter is mandatory and now put channel id in channelId text field  that we got in first step. Hit Authorize and Execute button we will get videos list and descriptions of the channel in JSON. Copy your URL that we are getting from GET Request. You can run this URL in browser as well by replacing {YOUR_API_KEY}.

 Youtube channel integration in Android:  Android Part

We will use Volley library to parse json data and populate into a listview. We have to add it in our build.gradle file of App.

Now download the YoutubeAndroidPlayerApi.jar file and copy this into your libs folder of Android project. If  the folder does not exist create a libs folder.

5.Create ChannelActivity  Activity: I named it as a activity_channel. In this activity we are using ListView to show you tube videos in a list. The XML code of this file is given below.

5.2 ChannelActivity.java: we are parsing JSON data  and using a custom adapter to supply data in a ListView. May be, you will get some error due to not getting classes just leave that and follow other next steps to create classes.

5.3. Create a videolist.xml layout: This is the layout to show video details of Youtube channel.

6. Create a CustomListAdapter class: This class extending BaseAdapter and populating data in a ListView. The code is given below.

7. Create an AppController class:  In this class we are creating an AppController class which is extending Application. We also need to define it in AndroidManifest file.

8.Create LruBitmapCache class: By using volley library downloading and caching of image is very simple. The code of this class is given below.

9. Create a VideoDetails class: In this class we are using setter and getter method to update and retrieve the values of videos.

10. Create a VideoActivity:  We will create an Activity to show Youtube Video, We are using YoutubePlayerView to show videos. This is our XML file of activity

10.2 VideoActivity.java: This Activity extends YouTubeBaseActivity. In this Activity we are getting video id from ChannelActivity and using curVideo() method to show the video.

11. Now add internet permission and define AppController in Manifest File.

Now run this App and enjoy 🙂 !!! If you are facing any problems let me know in a comment box. I will happy to help you. You can download this code from Github. You can also check other helpful tutorials.

Android Runtime Permission in Marshmallow

Login and Signup with PHP in Android

Parse JSON data in Spinner in Android

Demo: