Tutorial How to Play YouTube VIDEOS in Android Studio 3.1.1
Easy Tutorial step by step with free source code: Play YouTube Videos using a List with RecyclerView in Android Studio version 3.1.1. You can ADD as many videos as you want in List. For this tutorial I displayed 5 YouTube Videos.
Create new project with Application name: YouTube Videos App; Minimum SDK: API 27 – Android 8.1 (Oreo).
In Gradle Scripts->build.gradle (Module: app) write the code:
In app->java-> create New Java Class: YouTubeVideos and write the code:
public class YouTubeVideos {
String videoUrl;
public YouTubeVideos() {
}
public YouTubeVideos(String videoUrl) {
this.videoUrl = videoUrl;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
}
In app->java-> create New Java Class: VideoAdapter and write the code:
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import java.util.List;
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {
List<YouTubeVideos> youtubeVideoList;
public VideoAdapter() {
}
public VideoAdapter(List<YouTubeVideos> youtubeVideoList) {
this.youtubeVideoList = youtubeVideoList;
}
@Override
public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from( parent.getContext()).inflate(R.layout.video_view, parent, false);
return new VideoViewHolder(view);
}
@Override
public void onBindViewHolder(VideoViewHolder holder, int position) {
holder.videoWeb.loadData( youtubeVideoList.get(position).getVideoUrl(), "text/html" , "utf-8" );
}
@Override
public int getItemCount() {
return youtubeVideoList.size();
}
public class VideoViewHolder extends RecyclerView.ViewHolder{
WebView videoWeb;
public VideoViewHolder(View itemView) {
super(itemView);
videoWeb = (WebView) itemView.findViewById(R.id.videoWebView);
videoWeb.getSettings().setJavaScriptEnabled(true);
videoWeb.setWebChromeClient(new WebChromeClient() {
} );
}
}
}
In app->java->MainActivity.java write the code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.Vector;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Vector<YouTubeVideos> youtubeVideos = new Vector<YouTubeVideos>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager( new LinearLayoutManager(this));
youtubeVideos.add( new YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/eWEF1Zrmdow\" frameborder=\"0\" allowfullscreen></iframe>") );
youtubeVideos.add( new YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/KyJ71G2UxTQ\" frameborder=\"0\" allowfullscreen></iframe>") );
youtubeVideos.add( new YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/y8Rr39jKFKU\" frameborder=\"0\" allowfullscreen></iframe>") );
youtubeVideos.add( new YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/8Hg1tqIwIfI\" frameborder=\"0\" allowfullscreen></iframe>") );
youtubeVideos.add( new YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/uhQ7mh_o_cM\" frameborder=\"0\" allowfullscreen></iframe>") );
VideoAdapter videoAdapter = new VideoAdapter(youtubeVideos);
recyclerView.setAdapter(videoAdapter);
}
}
Run app in mobile device or Emulator (API 27–Android 8.1) and watch videos from YouTube in format portrait or landscape.
Follow Video TutorialHow to Play YouTube VIDEOS in Android Studio:
Download free MyYouTubeVideos application and install it on your mobile device to see how it works:
https://android-coffee.com/wp-content/uploads/projecs/MyYouTubeVideos.apkDownload project with Source Code from here:
https://android-coffee.com/wp-content/uploads/projecs/MyYouTubeVideosApp.7z
Please subscribe here to our YouTube channel. Thank you!
Check next tutorials to learn all about Android.
For furtherquestionsleave a message.
Why cant i link it from another activity using intent? I wanna link it to my listview so that when i click it, it will move to this page whr the videos are displayed
Hello! I used your code and I got this error:
The webpage at: “youtube link” could not be loaded because: net:: ERR_ACCESS_DENIED.
Do you have any solution for this? Thank you so much!
How to create a button that when you click on it; it take to another activity; that play on it the youtube video? Thanks.
hey, u know how to take on another activity ,send code…modivedant39@gmail.com
Why cant i link it from another activity using intent? I wanna link it to my listview so that when i click it, it will move to this page whr the videos are displayed
Here is the tutorial how to play YouTube videos from another activity:
https://youtu.be/0pOpDFsIcGk
Puedo ver leer una lista que contenga las url pero con la api de yputube en vez de utilizar el webview
how to enable full screen???
This, how to enable full screen?
Hello! I used your code and I got this error:
The webpage at: “youtube link” could not be loaded because: net:: ERR_ACCESS_DENIED.
Do you have any solution for this? Thank you so much!