Play YouTube Videos in Another Activity on Button Click from Main Activity in Android Studio
Tutorial how to link the list with RecyclerView with YouTube videos from another activity with button click, using intent, in Android Studio version 3.1.3. 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: Play YouTube Videos; 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:
package com.app.toyo.playyoutubevideos;
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:
package com.app.toyo.playyoutubevideos;
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:
package com.app.toyo.playyoutubevideos;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View v = findViewById(R.id.button1); v.setOnClickListener(this);
}
@Override public void onClick(View arg0) { if (arg0.getId() == R.id.button1) { //define a new Intent for the second Activity Intent intent = new Intent(this, SecondActivity.class); //start the second Activity this.startActivity(intent); } }
}
In app->java create New -> Java Class: SecondActivity.java and write the code:
package com.app.toyo.playyoutubevideos;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.Vector;
public class SecondActivity extends AppCompatActivity {
RecyclerView recyclerView;
Vector<YouTubeVideos> youtubeVideos = new Vector<YouTubeVideos>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
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);
}
}
In app->res->layout create New -> Layout resource file: activity_second.xml and write the code:
Run app in mobile device or Emulator (API 27 – Android 8.1) and click button from main activity to open YouTube Videos List in second activity.
Follow VideoTutorial:
Download project with Source Code from here:
https://android-coffee.com/wp-content/uploads/projecs/PlayYouTubeVideos.7z
Please subscribe here to our YouTube channel. Thank you!
Check next tutorials to learn all about Android.
For furtherquestionsleave a message.
i have a problem that every thing is working but the video is blank , the sound is working very well but with a blank screen
please if you can help me