পূর্ব প্রস্তুতি
এই উদাহরণটি RecyclerView এবং WebView ব্যবহার করে Android অ্যাপে YouTube ভিডিও এমবেড করার পদ্ধতি দেখায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project থেকে একটি নতুন প্রজেক্ট শুরু করুন।
ধাপ ২: activity_main.xml-এ RecyclerView যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ধাপ ৩: MainActivity.kt-এ ডেটা এবং অ্যাডাপ্টার সেটআপ করুন
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.*
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private val youtubeVideos = ArrayList<YouTubeVideos>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
recyclerView = findViewById(R.id.recyclerView)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this)
// YouTube iframe URLs যোগ করুন
youtubeVideos.add(YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/X7SiuQxhAjg\" frameborder=\"0\" allowfullscreen></iframe>"))
youtubeVideos.add(YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/KyJ71G2UxTQ\" frameborder=\"0\" allowfullscreen></iframe>"))
youtubeVideos.add(YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/y8Rr39jKFKU\" frameborder=\"0\" allowfullscreen></iframe>"))
youtubeVideos.add(YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/8Hg1tqIwIfI\" frameborder=\"0\" allowfullscreen></iframe>"))
youtubeVideos.add(YouTubeVideos("<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/uhQ7mh_o_cM\" frameborder=\"0\" allowfullscreen></iframe>"))
val videoAdapter = VideoAdapter(youtubeVideos)
recyclerView.adapter = videoAdapter
}
}
ধাপ ৪: VideoAdapter.kt ক্লাস তৈরি করুন
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.recyclerview.widget.RecyclerView
class VideoAdapter(private val youtubeVideoList: List<YouTubeVideos>) :
RecyclerView.Adapter<VideoAdapter.VideoViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.video_view, parent, false)
return VideoViewHolder(view)
}
override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
holder.videoWeb.loadData(youtubeVideoList[position].videoUrl, "text/html", "utf-8")
}
inner class VideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var videoWeb: WebView = itemView.findViewById(R.id.webView)
init {
videoWeb.settings.javaScriptEnabled = true
videoWeb.webChromeClient = WebChromeClient()
}
}
override fun getItemCount(): Int = youtubeVideoList.size
}
ধাপ ৫: YouTubeVideos.kt ডেটা ক্লাস
class YouTubeVideos(var videoUrl: String?)
ধাপ ৬: video_view.xml লেআউট ফাইল
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="180dp">
</WebView>
ধাপ ৭: AndroidManifest.xml-এ ইন্টারনেট পারমিশন যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.youtubeplayer">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
অ্যাপ্লিকেশন চালান
ডিভাইস কানেক্ট করে Run বাটন চাপুন। RecyclerView-এ YouTube ভিডিওগুলো লোড হয়ে দেখাবে।