I having problems with Webview Jetpack Compose iFrame video full screen. I want to use this function in my coding but I dont know how to implement it to my coding.I new in android development. Need help.
This is my coding.
MainActivity.kt
package com.example.runningman
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.runningman.ui.theme.RunningManTheme
import com.google.accompanist.web.AccompanistWebChromeClient
import com.google.accompanist.web.rememberWebViewNavigator
import com.google.accompanist.web.rememberWebViewState
import com.google.accompanist.web.WebView
import com.google.accompanist.web.rememberWebViewStateWithHTMLData
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Calling the composable function
// to display element and its contents
MyContent(MainActivity())
}
}
}
@Composable
fun MyContent(activity: ComponentActivity) {
val frameVideo = "<html><body><iframe width=\"300\" height=\"320\" src=\"https://www.youtube.com/embed/gTzSw_xVCWI\" title=\"Alec Benjamin - Must Have Been The Wind (Lyrics)\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen></iframe></body></html>"
val state = rememberWebViewState(url = frameVideo)
val navigator = rememberWebViewNavigator()
val chromeClient = remember { CustomChromeClient(activity) }
WebView(
state = rememberWebViewStateWithHTMLData(data = frameVideo),
modifier = Modifier.fillMaxSize(),
navigator = navigator,
chromeClient = chromeClient, //This is what concerns us for fullscreen mode
onCreated = { webView ->
webView.settings.javaScriptEnabled=true
webView.loadData(frameVideo, "text/html", "utf-8")
//configuring the webview settings here such as enabling javascript
}
)
}
class CustomChromeClient(val activity: ComponentActivity): AccompanistWebChromeClient() {
var customView: View? = null
override fun onHideCustomView() {
(activity.window.decorView as FrameLayout).removeView(this.customView)
this.customView = null
}
override fun onShowCustomView(paramView: View, paramCustomViewCallback: CustomViewCallback) {
if (this.customView != null) {
onHideCustomView()
return
}
this.customView = paramView
(activity.window.decorView as FrameLayout).addView(this.customView, FrameLayout.LayoutParams(-1, -1))
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:hardwareAccelerated="true"
android:configChanges="orientation|screenSize"
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.RunningMan"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.RunningMan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat
2023-08-29 22:25:13.610 17532-18686 Codec2Client com.example.runningman D setOutputSurface -- failed to set consumer usage (6/BAD_INDEX)
2023-08-29 22:25:13.610 17532-18686 Codec2Client com.example.runningman D setOutputSurface -- generation=17952769 consumer usage=0x900
2023-08-29 22:25:13.635 17532-18686 Codec2Client com.example.runningman D Surface configure completed
2023-08-29 22:25:13.636 17532-18686 DMABUFHEAPS com.example.runningman I Using DMA-BUF heap named: system
2023-08-29 22:25:13.693 17532-17532 System.err com.example.runningman W java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.getDecorView()' on a null object reference
2023-08-29 22:25:13.698 17532-17532 System.err com.example.runningman W at com.example.runningman.CustomChromeClient.onShowCustomView(MainActivity.kt:71)
2023-08-29 22:25:13.699 17532-17532 System.err com.example.runningman W at Qb.enterFullscreenModeForTab(chromium-TrichromeWebViewGoogle6432.apk-stable-567263637:155)
2023-08-29 22:25:13.699 17532-17532 System.err com.example.runningman W at android.os.MessageQueue.nativePollOnce(Native Method)
2023-08-29 22:25:13.699 17532-17532 System.err com.example.runningman W at android.os.MessageQueue.next(MessageQueue.java:335)
2023-08-29 22:25:13.699 17532-17532 System.err com.example.runningman W at android.os.Looper.loopOnce(Looper.java:162)
2023-08-29 22:25:13.700 17532-17532 System.err com.example.runningman W at android.os.Looper.loop(Looper.java:294)
2023-08-29 22:25:13.700 17532-17532 System.err com.example.runningman W at android.app.ActivityThread.main(ActivityThread.java:8177)
2023-08-29 22:25:13.700 17532-17532 System.err com.example.runningman W at java.lang.reflect.Method.invoke(Native Method)
2023-08-29 22:25:13.701 17532-17532 System.err com.example.runningman W at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
2023-08-29 22:25:13.701 17532-17532 System.err com.example.runningman W at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
2023-08-29 22:25:13.715 17532-17532 chromium com.example.runningman A [FATAL:jni_android.cc(289)] Please include Java exception stack in crash report
2023-08-29 22:25:14.812 17532-17532 libc com.example.runningman A Fatal signal 5 (SIGTRAP), code 128 (SI_KERNEL), fault addr 0x0 in tid 17532 (mple.runningman), pid 17532 (mple.runningman)
---------------------------- PROCESS STARTED (18735) for package com.example.runningman ----------------------------
2023-08-29 22:25:15.696 18733-18733 DEBUG pid-18733 A Cmdline: com.example.runningman
2023-08-29 22:25:15.696 18733-18733 DEBUG pid-18733 A pid: 17532, tid: 17532, name: mple.runningman >>> com.example.runningman <<<
---------------------------- PROCESS ENDED (17532) for package com.example.runningman ----------------------------
I expected this full screen function are implemented in my coding