Hey this's an old question, but here its my updated solution. Im using Kotlin and AndroidX.
First, you need two views inside your 'video activity layout', something like these:
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/videoFrame"
android:layout_width="0dp"
android:layout_height="0dp"
android:elevation="10dp" android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<WebView
android:id="@+id/webview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
You'll use videoFrame frame layout for setting extra fullscreen content and obviously a webview for your web content.
Then, use a method to load an url to your webview:
private fun initWebView(webUrl : String){
val webView : WebView? = this.findViewById(R.id.webview)
val videoFrame : FrameLayout? = this.findViewById(R.id.videoFrame)
webView?.settings?.apply {
mediaPlaybackRequiresUserGesture = true
javaScriptEnabled = true //use this carefully
}
webView?.webChromeClient = object : WebChromeClient () {
override fun onProgressChanged(view: WebView?, newProgress: Int) {
Log.d(TAG, "WebChromeClient, onProgressChanged newProgress=$newProgress") }
override fun onShowCustomView(fullScreenContent: View?, callback: CustomViewCallback?) {
super.onShowCustomView(fullScreenContent, callback)
Log.d(TAG, "onShowCustomView")
fullScreenContent?.let {fullScreenView ->
videoFrame?.removeAllViews()
videoFrame?.visibility = View.VISIBLE
videoFrame?.addView(fullScreenView)
}
}
override fun onHideCustomView() {
super.onHideCustomView()
Log.d(TAG, "onShowCustomView")
videoFrame?.visibility = View.GONE
videoFrame?.removeAllViews()
}
}
webView?.loadUrl(webUrl)
}
The magic happens when you overrite onShowCustomView and onHideCustomView methods.
Just add fullScreenContent into videoFrame layout.
And that's all.
Its working (and tested) on Android 9.