Jenna here, I've looked for the past month all over and still can't find answer to this. I've looked through ever question here on stack overflow. if anyone can please help. I honestly can't find a way to upload my sample project, its really small, if anyone has way please let me know or I can also just directly send it to you.
First off I want to say I was able to get this accomplished before using default fragment created by android studio but when I create a custom fragment I have no luck and this was 6 months ago. for some reason its not working either way now. I have a navigation drawer view activity.On the first fragment HomeFragment I have a button, when you click it, it will navigate you to a custom created WebViewFragement. The webview loads up perfectly however when I click a link that takes me to a page that seems to be loading css, java, html, the page takes me to white screen only. I've tried many ways to implement this webview for the past few weeks, so now im asking if anyone here can help. When the fragment loads it loads up https://dominos.com (I've also tried using the https://www.dominos.com. when I click any of the buttons/links with the red arrows, the webview loads up white page. when I click the button with the green arrow it loads up that page because it doesn't have such complicated html/css..etc. (at least I think thats what the difference is. (https://i.stack.imgur.com/r0v3e.jpg) the page that is suppose to load up when you click any of the buttons that red arrows are pointing to is this page (https://i.stack.imgur.com/ZPjYq.png)
when I click any of the buttons/links with the red arrows, the webview loads up white page. when I click the button with the green arrow it loads up that page because it doesn't have such complicated html/css..etc. (at least I think thats what the difference is.
(https://i.stack.imgur.com/r0v3e.jpg)
the page that is suppose to load up when you click any of the buttons that red arrows are pointing to is this page
(https://i.stack.imgur.com/ZPjYq.png)
Here is code for my first fragment called HomeFragment
package com.dominospizza.couponsgames.ui.home
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import com.dominospizza.couponsgames.R
import com.dominospizza.couponsgames.databinding.FragmentHomeBinding
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
private lateinit var myWebView: WebView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root
val textView: TextView = binding.textHome
homeViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}
binding.button.setOnClickListener { view ->
view.findNavController().navigate(R.id.action_nav_home_to_webViewFragment)
}
return root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
here is code for my custom WebViewFragment
package com.dominospizza.gamescoupons
import android.net.http.SslError
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.webkit.SslErrorHandler
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.fragment.app.Fragment
import com.dominospizza.couponsgames.R
import com.dominospizza.couponsgames.databinding.FragmentWebViewBinding
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
class WebViewFragment : Fragment() {
private var _binding: FragmentWebViewBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentWebViewBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val myWebView: WebView = view.findViewById(R.id.webView)
// Enable debugging of web contents
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true)
}
// Set WebViewClient and WebChromeClient
myWebView.webChromeClient = WebChromeClient()
myWebView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
myWebView.loadUrl(request?.url.toString())
return true
}
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
var message = "SSL Certificate error."
when (error.primaryError) {
SslError.SSL_UNTRUSTED ->
message = "The certificate authority is not trusted."
SslError.SSL_EXPIRED ->
message = "The certificate has expired."
SslError.SSL_IDMISMATCH ->
message = "The certificate Hostname mismatch."
SslError.SSL_NOTYETVALID ->
message = "The certificate is not yet valid."
}
message += "\"SSL Certificate Error\" Do you want to continue anyway?"
//Log your message
handler.proceed()
}
}
val userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
val webSetting: WebSettings = myWebView.settings
webSetting.javaScriptEnabled = true
webSetting.allowContentAccess = true
webSetting.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
webSetting.domStorageEnabled = true
webSetting.useWideViewPort = true
webSetting.allowFileAccess = true
webSetting.setGeolocationEnabled(true)
webSetting.userAgentString = userAgent
myWebView.isScrollbarFadingEnabled = false
myWebView.setInitialScale(1)
myWebView.settings.loadsImagesAutomatically = true
myWebView.settings.loadWithOverviewMode = true
myWebView.settings.cacheMode = WebSettings.LOAD_DEFAULT
myWebView.settings.setSupportMultipleWindows(true)
myWebView.settings.setSupportZoom(true)
myWebView.settings.javaScriptCanOpenWindowsAutomatically = true
myWebView.webViewClient = WebViewClient()
myWebView.clearCache(true)
myWebView.clearHistory()
myWebView.clearCache(true)
myWebView.clearHistory()
myWebView.loadUrl("https://dominos.com")
myWebView.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_BACK && event.action == MotionEvent.ACTION_UP && myWebView.canGoBack()) {
myWebView.goBack()
return@OnKeyListener true
}
false
})
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
here is version of android studio if that helps.
Build gradle for app
`plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.dominospizza.couponsgames'
compileSdk 33
defaultConfig {
applicationId "com.dominospizza.couponsgames"
minSdk 22
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
viewBinding {
enabled = true
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
Build gradle for project
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.0.0' apply false
id 'com.android.library' version '8.0.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}
here is the logcat
2023-04-23 02:19:02.728 3891-3891 HwViewRootImpl com.dominospizza.couponsgames I removeInvalidNode all the node in jank list is out of time 2023-04-23 02:19:02.730 3891-3891 AudioManager com.dominospizza.couponsgames V querySoundEffectsEnabled... 2023-04-23 02:19:02.756 3891-3891 SysUtils com.dominospizza.couponsgames I hw theme is enabled(false) 2023-04-23 02:19:02.756 3891-3891 AwSettings com.dominospizza.couponsgames I selectHandleChangeEnbled(false), EMUI_Version: 27 2023-04-23 02:19:02.760 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:aw_contents.cc(716)] JNI_AwContents_Init. 2023-04-23 02:19:02.764 3891-3891 AwSettings com.dominospizza.couponsgames I getEffectiveBackgroundColor forceDarkMode mUseHwDarkMode = true 2023-04-23 02:19:02.765 3891-3891 AwSettings com.dominospizza.couponsgames I getAdBlockEnabled:false this = org.chromium.android_webview.AwSettings@3739cbe 2023-04-23 02:19:02.783 3891-3891 AwSettings com.dominospizza.couponsgames I getEffectiveBackgroundColor forceDarkMode mUseHwDarkMode = true 2023-04-23 02:19:02.786 3891-3891 chatty com.dominospizza.couponsgames I uid=10154(com.dominospizza.couponsgames) identical 7 lines 2023-04-23 02:19:02.791 3891-3891 AwSettings com.dominospizza.couponsgames I getEffectiveBackgroundColor forceDarkMode mUseHwDarkMode = true 2023-04-23 02:19:02.791 3891-3891 AwSettings com.dominospizza.couponsgames I getAdBlockEnabled:false this = org.chromium.android_webview.AwSettings@3739cbe 2023-04-23 02:19:02.950 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:navigation_request.cc(2192)] [AdBlock] Redirect to https//, try to get adblock switch from UI. 2023-04-23 02:19:03.088 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:navigation_request.cc(2192)] [AdBlock] Redirect to https//, try to get adblock switch from UI. 2023-04-23 02:19:03.757 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(27)] enter ShouldDoAdaptiveRelayout 2023-04-23 02:19:03.758 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(37)] exit ShouldDoAdaptiveRelayout, should[0] 2023-04-23 02:19:08.842 3891-3891 HwViewRootImpl com.dominospizza.couponsgames I removeInvalidNode all the node in jank list is out of time 2023-04-23 02:19:10.801 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(27)] enter ShouldDoAdaptiveRelayout 2023-04-23 02:19:10.802 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(37)] exit ShouldDoAdaptiveRelayout, should[0] 2023-04-23 02:19:11.078 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(27)] enter ShouldDoAdaptiveRelayout 2023-04-23 02:19:11.078 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(37)] exit ShouldDoAdaptiveRelayout, should[0] 2023-04-23 02:19:11.520 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:gesture_detector.cc(630)] Stop drag long press timer. 2023-04-23 02:19:11.573 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(27)] enter ShouldDoAdaptiveRelayout 2023-04-23 02:19:11.574 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(37)] exit ShouldDoAdaptiveRelayout, should[0] 2023-04-23 02:19:11.606 3891-3891 InputMethodManager com.dominospizza.couponsgames W startInputReason = 4 2023-04-23 02:19:11.608 3891-3891 HwRemoteIn...hodManager com.dominospizza.couponsgames W isCasting false because IHwDistributedWindowManager is invalid. 2023-04-23 02:19:11.893 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(27)] enter ShouldDoAdaptiveRelayout 2023-04-23 02:19:11.893 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(37)] exit ShouldDoAdaptiveRelayout, should[0] 2023-04-23 02:19:11.912 3891-4167 hwschromium-3355 com.dominospizza.couponsgames I [INFO:network_service_network_delegate.cc(161)] INFO: resource : https//*** error code: 400 2023-04-23 02:19:11.928 3891-4167 hwschromium-3355 com.dominospizza.couponsgames I [INFO:network_service_network_delegate.cc(246)] final url https//*** error_code -3 ip address: ignored 2023-04-23 02:19:11.931 3891-4167 chatty com.dominospizza.couponsgames I uid=10154(com.dominospizza.couponsgames) NetworkService identical 2 lines 2023-04-23 02:19:11.932 3891-4167 hwschromium-3355 com.dominospizza.couponsgames I [INFO:network_service_network_delegate.cc(246)] final url https//*** error_code -3 ip address: ignored 2023-04-23 02:19:11.940 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(27)] enter ShouldDoAdaptiveRelayout 2023-04-23 02:19:11.940 3891-3891 hwschromium-3355 com.dominospizza.couponsgames I [INFO:adaptive_relayout_impl.cc(37)] exit ShouldDoAdaptiveRelayout, should[0] 2023-04-23 02:19:12.048 3891-4167 hwschromium-3355 com.dominospizza.couponsgames W [WARNING:spdy_session.cc(3391)] Received HEADERS for invalid stream 5